Why is typeid not compile-time constant like sizeof
Asked Answered
C

5

5

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)

Condolence answered 30/12, 2009 at 17:41 Comment(0)
N
10

When you are dealing with types, you'd rather use simple metaprogramming techniques:

#include <type_traits>

template <class T>
void Foo()
{
    static_assert((std::is_same<T, int>::value || std::is_same<T, double>::value));
}

int main()
{
    Foo<int>();
    Foo<float>();
}

where is_same could be implemented like this:

template <class A, class B>
struct is_same
{
    static const bool value = false;
};

template <class A>
struct is_same<A, A>
{
    static const bool value = true;
};

typeid probably 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).

Nardi answered 30/12, 2009 at 20:33 Comment(1)
BTW, is_same is a part of Standard library since C++11, as well as static_assert.Alanna
F
5

C++ can handle constant (compile-time) expressions of some types, but reference types are not among those types. The result of a typeid expression is a reference to a std::type_info object.

Apparently for a while in 2008, the C++ standard committee had typeid expressions such as the ones in your example behaving as constant expressions, just like sizeof. However, according to this comment, that change was ultimately reverted.

Flavory answered 30/12, 2009 at 18:21 Comment(2)
the link to comment is dead!Inappreciable
i want to create a template of std::type_index or something that uniquely identifies a class. How to do that?Inappreciable
P
2

Because typeid requires RTTI, i.e, typeid is performed at runtime and BOOST_STATIC_ASSERT is performed at compile time.

More information here.

Pilsen answered 30/12, 2009 at 17:45 Comment(4)
except smerlin was asking about a particular use of typeid, and this particular use of typeid does not do runtime lookupSleigh
I disagree, his question: "Why is typeid(someType) not constant like sizeof(someType) ?", and not about a particular use of typeidPilsen
Coelhudo, the "particular use" is the one in which the argument is a type. RTTI is only used when the argument is an object expression.Algernon
And then RTTI is also only used if the object expression is of polymorphic type. In other words, pair<int, int> p; typeid(p); does not need RTTI.Pasadena
N
1

Because typeid has the flexibility to do lookups at runtime based on a pointer or reference to an object, it can't return a compile-time constant. Even when it looks like it could. sizeof has no such restrictions, as it always does its calculation at compile time.

Nelson answered 30/12, 2009 at 17:54 Comment(3)
except smerlin was asking about a particular use of typeid, and this particular use of type id does not do runtime lookup.Sleigh
Runtime constants are generally limited to integer values. typeid returns a reference to a type_info class, which is much more than that.Nelson
Ah i forgot that typeid works for objects aswell ... still i think it would be very useful if something like the example above could be done with it. Hopefully C++0x supports something like that. Well at least it will support static asserts...Condolence
F
0

It knows the type itself (in its own internal language) at compile time, but not its type id (which has clearly been created for runtime).

Funch answered 30/12, 2009 at 17:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.