Go Down

Topic: Error: cannot use typeid with -fno-rtti (Read 903 times) previous topic - next topic

guix

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

Is there a solution ?

pYro_65

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

here is a use.
Code: [Select]

//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:

Code: [Select]
template< typename _A, typename _B > bool CompareType( _A a, _B b )
  {
    return false;
  }
 
template< typename _A > bool CompareType( _A a, _A b )
  {
    return true;
  }


Code: [Select]
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.

guix

#2
Nov 26, 2012, 06:18 pm Last Edit: Nov 26, 2012, 06:20 pm by guix Reason: 1
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:

Code: [Select]

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


I believe it's not possible...I tried:
Code: [Select]

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!

PaulS

Quote
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?

PeterH


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?
I only provide help via the forum - please do not contact me for private consultancy.

pYro_65

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:

Code: [Select]
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:
Code: [Select]


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

      case 2:
        break;
    }
    return true;
  }

guix

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 :)

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview