Sat
21
Nov 2015
I was thinking recently about passing raw data pointers (like void* or char*), which we have to do sometimes in C++. Is there any way to check if the type that we cast it to is the same as the type of last assigned value? I came up with an idea of implementing a "smart pointer" class using RTTI (specifically typeof operator) to store type information next to the actual pointer. Example usage:
int i = 123;
type_safe_ptr ptr{&i};
int j = *ptr.get_typed<int>(); // OK
float f = *ptr.get_typed<float>(); // Error
Initially I wanted to store pointer to const type_info struct returned by typeid operator, and it seems to work in Visual Studio 2015, but language standard defines the object returned by typeid as temporary, so it is not formally correct. Finally I decided to store typeid(T).hash_code(). You can find my implementation of classes type_safe_ptr and type_safe_const_ptr in file: type_safe_ptr.hpp. Here is example tesing program:
#include <cstdio>
#include "type_safe_ptr.hpp"
int main()
{
type_safe_ptr ptr1;
// ptr1 is null.
assert(!ptr1);
assert(ptr1.get() == nullptr);
int i = 123;
type_safe_ptr ptr2{&i};
// ptr2 is pointer to int.
assert(*ptr2.get_typed<int>() == 123);
// It would activate assert inside type_safe_ptr.get_typed, because ptr2 is int not float.
//assert(*ptr2.get_typed<float>() == 123.f);
struct STest { int i; } obj;
// itr2 is now pointer to STest.
ptr2 = &obj;
ptr2.get_typed<STest>()->i = 124;
assert(obj.i == 124);
// It would activate assert inside type_safe_ptr.get_typed, because ptr2 is now STest not int.
//assert(*ptr2.get_typed<int>() == 123);
type_safe_const_ptr cptr = type_safe_const_ptr(ptr2);
// cptr is pointer to const STest.
assert(cptr.get_typed<STest>()->i == 124);
const int* constIntPtr = &i;
cptr.reset(constIntPtr);
// cptr is now pointer to const int.
assert(*cptr.get_typed<int>() == 123);
}
Some issues and open question regarding my solution are:
Final question is, whether this whole idea of "type-checking void* smart pointer" makes any sense? I am not sure about that, but anyway it was a funny experiment :)
Comments | #c++ #visual studio Share