static_cast and typeid

Is there a way to pass a typeid return directly to a cast (with some clunky if else if block)? Something similar to the following is what I'm wondering about (but that works!):

1
2
3
4
int i(0);
float j(0.0f);

i = static_cast<typeid(i)>(j);


If this is not possible with typeid is there and equivalent function that will allow dynamic determination of type in a form that is acceptable by a casting operator?

Thanks in advance for your help with this!
Last edited on
C++ is a statically typed language, meaning that types of variables are always known at compiler time and such thing would never be needed. In some cases though, you may not know the real type of an object being pointed to (that is the case Base* my_base_ptr = &my_object_derived_from_base;). Then you can use dynamic_cast to see if that Base* really points to a Derived object.
This is done with a different keyword, decltype (part of C++11, supported by Visual Studio 2010, gcc 4.4+, and most other modern compilers)

1
2
3
int i(0);
float j(0.0f);
i = static_cast<decltype(i)>(j);


It's not dynamic though, it is substituted by the static type of the expression (in this case, i)
Last edited on
hamsterman, what I'm trying to do is use a template function which treats different types differently, and I was hoping to do this within a switch statement rather than specialized templates due to the number of options (smaller SLOC this way).

Cubbi, thank you that is exactly what I'm looking for! Unfortunately our current project compiler does not support C++11 as of this point, so I guess in the meantime I'll have to use specialization for now. I'll make a note of this so I can use it as soon as available.

On a side note, would specialization be more optimized than using decltype within a switch statement? If so maybe I should go ahead with the specialization even when C++11 becomes available to me.
Yes, because it's resolved at compile time.
decltype is also resolved at compile time
can decltype differentiate between typedefs as well? I know template specialization cannot.

ie.
1
2
typedef int type1_t;
typedef int type2_t;  // does decltype think this is the same as type1_t? 

Sorry, since I do not have a C++11 compiler I cannot check this myself!
typedef doesn't create new types. It only gives an alternative name for the type so there is no way to tell them apart because they are the same type.
what I'm trying to do is use a template function which treats different types differently
Then it is not a template.
Again, unless you're dealing with polymorphism, type is known at compile time. You don't need anything dynamic. And since you considered template specializations, you must understand that.
To save lines of code, I suggests this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
 
template<class T>
struct AmIInteger {
   void am_i_integer() { std::cout << "I'm not an integer\n"; }
};
 
template<>
struct AmIInteger<int> {
   void am_i_integer() { std::cout << "Yes!!\n"; }
};
 
template<class T>
class Foo : public AmIInteger<T> {
   public:
   void some_big_function() {}
};
 
int main() {
   Foo<int> i;
   Foo<float> f;
   i.am_i_integer();
   f.am_i_integer();
}
Though I am not sure if this is a logical design choice. You can also do this with ifs (like you wanted):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <typeinfo>
 
template<class T>
class Foo {
   public:
   void some_big_function() {}
   void am_i_integer() {
      if (typeid(int) == typeid(T)) std::cout << "Yes!!\n";
      else std::cout << "I'm not an integer\n";
   }
};
 
int main() {
   Foo<int> i;
   Foo<float> f;
   i.am_i_integer();
   f.am_i_integer();
}

Thank you everyone for the help!
Topic archived. No new replies allowed.