Pages: [1]   Go Down
Author Topic: Error: cannot use typeid with -fno-rtti  (Read 618 times)
0 Members and 1 Guest are viewing this topic.
France
Offline Offline
God Member
*****
Karma: 29
Posts: 898
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Is there a solution ?
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1782
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

here is a use.
Code:
//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:
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:
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.
Logged


France
Offline Offline
God Member
*****
Karma: 29
Posts: 898
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void func( int value )
{
  switch typeof( value )
  {
        case long:
...

I believe it's not possible...I tried:
Code:
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!
« Last Edit: November 26, 2012, 12:20:37 pm by guix » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11158
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1782
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

      case 2:
        break;
    }
    return true;
  }
Logged


France
Offline Offline
God Member
*****
Karma: 29
Posts: 898
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Pages: [1]   Go Up
Jump to: