Why is typeid(someType) not constant like sizeof(someType) ?
This question came up because recently i tried something like:
template <class T>
class Foo
{
static_assert(typeid(T)==typeid(Bar) || typeid(T)==typeid(FooBar));
};
And i am curious why the compiler knows the size of types (sizeof) at compile time, but not the type itself (typeid)
When you are dealing with types, you’d rather use simple metaprogramming techniques:
where
is_samecould be implemented like this:typeidprobably isn’t compile-time because it has to deal with runtime polymorphic objects, and that is where you’d rather use it (if at all).