Error: cannot use typeid with -fno-rtti

Hello, I try to use the typeid function but I get this error: cannot use typeid with -fno-rtti

Is there a solution ?

1 Like

There is a GCC specific 'typeof', not the same though

here is a use.

//type based on value.
typeof( 123 ) a_byte;
typeof( 12345 ) an_int;

typedef typeof( 123456 ) MyType;

If you wish to compare types, here is one of my utility functions:

template< typename _A, typename _B > bool CompareType( _A a, _B b )
  {
    return false;
  }
  
template< typename _A > bool CompareType( _A a, _A b )
  {
    return true;
  }
CompareType( 1, 2.0f ); //false
CompareType( 'c', 'g' ); //true

You can use it in combination with typeof for #defines or functions that don't know the incoming type.

I see, thanks for this, but that's not really what I need, in fact I want a way to make a function that can handle different types of parameter, like for example:

void func( int value )
{
  switch typeof( value )
  {
        case long:
...

I believe it's not possible...I tried:

void func( int value )
{
  switch sizeof( value )
  {
        case 4:
...

But the size is always, obviously, the size of an int.

I believe I have no other choice than using function overloading!

1 Like

in fact I want a way to make a function that can handle different types of parameter, like for example:

The variable in the typeof() statement is a known type. Switching on the result seems pointless, even if the switch statement supported that. Which it doesn't.

Why not do what everyone else does and use function overloading in a class?

guix:
I see, thanks for this, but that's not really what I need, in fact I want a way to make a function that can handle different types of parameter, like for example:

I don't often need to resort to rtti so there may be some trick I'm missing here, but I'm struggling to imagine how you'd use your example even supposing you managed to achieve the type lookup that you're stuck on. The only thing that I can imagine being useful is doing type recovery on a superclass, but how would that ever be useful using a primitive value? Any type coercion going on would be done in the calling code, and by the time your function gets to execute you know that the value is of the type defined by the function prototype. It's not as if 'int' is a subtype of 'long int', or anything like that. So, what is it that you're trying to achieve?

No RTTI is needed, it is a runtime feature for runtime use, a switch is a compile time statement, all cases have to be constant, which makes RTTI an incredibly redundant overhead.

Like paulS said, overload, then the size is implicitly known:

bool Foo( char c )
  {
    //no switch needed, type size known
    return true;
  }

bool Foo( int i )
  {
    //no switch needed, type size known
    return true;
  }

Or templates:

template< typename T > bool Foo( T t )
  {
    switch( sizeof( T ) ){
      case 1:
        break;

      case 2:
        break;
    }
    return true;
  }

Yep I ended up using overloading, I was scared about mem usage but then I broke the function in smaller ones to be called in the big one so most of the code isn't duplicated... Thanks all anyway :slight_smile: