1

In C++ is there a way to convert a type to an integer at compile-time (maybe with typeid) ? My goal is to pass a unique code for each type in that class :

template<int TypeCode>
class MyClass
{
};

EDIT : Some more details about what I am trying to do. In fact, MyClass will be more like that :

template<int Code>
class AbstractBase
{
};

I write a highly templated code with a lot of CRTP technique, and I need to check compatibilites between types for some operations. To do so, my idea was to inherit compatible types from the AbstractBase class specifying the same code for all these types. Using that, and simply calling std::enable_if<std::is_base_of<AbstractBase<MyCode>, T>::value>::typeI would be able to check for type compatibility for some operations.

At first order, I can generate a code by hand, but it would be more elegant, if I can generate this code from types automatically.

6
  • 1
    static_cast if it's properly convertible. typeid is runtime. Commented Dec 19, 2012 at 19:39
  • It's not possible to get a compile time integer from a type, but you can get runtime integer from an address bound to a type which can be then converted to an integer... Commented Dec 19, 2012 at 19:40
  • 3
    Can you explain a bit more about what you're trying to do here? Commented Dec 19, 2012 at 19:46
  • What info your program will have? Is it void*, unknown template typename T, or derived class? Commented Dec 19, 2012 at 19:47
  • What do you need a code for? Why doesn't the type suffice as a unique identifier of itself? :P Commented Dec 19, 2012 at 19:51

3 Answers 3

2

There are many methods. Here is template specialization:

#include<iostream>
using namespace std;

template<class T>   struct type_code        { enum{value=0}; };  // unknown type code
template<>          struct type_code <int>  { enum{value=1}; }; 
template<>          struct type_code <float>{ enum{value=2}; }; 

int main() {
        cout << type_code<void>::value << endl;
        cout << type_code<int>::value << endl;
        cout << type_code<float>::value << endl;
}

Output:

0
1
2
Sign up to request clarification or add additional context in comments.

Comments

0

Not sure if I understand you completely. Is this what you are talking about?

template<int TypeCode>
class MyClass
{
private:
    int i;
    double d;

public:
    template<typename T>
    operator T()
    {
        if(strcmp(typeid(T).name(),"int")==0)
            return i;
        else if(strcmp(typeid(T).name(),"double")==0)
            return d;
        // some other types here ...
    }
};

1 Comment

@Leonid - It is not, but he mentioned typeid in his question, which is RTTI.
0

Well, you can create a list of types, then extract the index of a type in that list, at compile time.

From another answer of mine, here is this technique:

#include <type_traits>

template<typename... Types>
struct Seq {};

template<typename T, typename Seq, typename=void>
struct IndexOf;

template<typename T, typename First, typename... Types>
struct IndexOf<T, Seq<First, Types...>, typename std::enable_if< std::is_same<T, First>::value >::type > {
  enum { value = 0 };
};
template<typename T, typename First, typename... Types>
struct IndexOf<T, Seq<First, Types...>, typename std::enable_if< !std::is_same<T, First>::value >::type > {
  enum { value = 1+IndexOf<T,Seq<Types...>>::value };
};

typedef Seq< bool, char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long > IntegerTypes;

#include <iostream>

int main() {
  std::cout << IndexOf< int, IntegerTypes >::value << "\n";
  // this next line will not compile, because void is not in the IntegerTypes sequence:
  // std::cout << IndexOf< void, IntegerTypes >::value << "\n";
}

where I'm using it on integers.

So if you have a list of types you want integers for, you can just list all the types, and the above technique will give a unique integer for each one (the reverse mapping is also relatively easy -- compile time index into the list to type).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.