I'm trying to implement a template class (named Get<> here) that, given a structure H, the type Get<H>::type is the H itself if the qualified-id H::der doesn't exist, and is Get<H::der>::type otherwise. I can't understand what's wrong with the following code:
#include <iostream>
#include <typeinfo>
using namespace std;
template<class U, class V = void>
struct Get
{
static const char id = 'A';
typedef U type;
};
template<class U>
struct Get<U,typename U::der>
{
static const char id = 'B';
typedef typename Get<typename U::der>::type type;
};
struct H1
{ };
struct H2
{ typedef double der; };
struct H3
{ typedef void der; };
struct H4
{ typedef H2 der; };
void print(char id, const char* name)
{
cout << id << ", " << name << endl;
}
int main(int , char *[])
{
print(Get<H1>::id, typeid(Get<H1>::type).name()); // prints "A, 2H1", OK
print(Get<H2>::id, typeid(Get<H2>::type).name()); // prints "A, 2H2", why?
print(Get<H3>::id, typeid(Get<H3>::type).name()); // prints "B, v" , OK
print(Get<H4>::id, typeid(Get<H4>::type).name()); // prints "A, 2H4", why?
}
I'd like some help to make this code behave as expected. More specifically, I wish that Get< H2 >::type was equal to double, and the same for Get< H4 >::type.
The template Get<> has a default template parameter -- this is very dangerous. Depending on whether V is equal int, void or double you get different results. This is what happens:
Get<H2>::type is Get<H2, void> on the first place (with id='A'). Now comes the check, whether there is an specialization of it. Your B is Get<U,typename U::der> which becomes Get<U, double>. But this doesn't match whith Get<H2, void>, so A is chosen. Things get interesting with Get<H2>::type. Then the variant B is also Get<U, void> and provides a better match. However, the approach can't work for all types.
This is how I would have implemented Get:
template<class U>
class Get
{
template <typename T, typename = typename T::der>
static typename Get<typename T::der>::type test(int);
template <typename T>
static T test(...);
public:
typedef decltype(test<U>(0)) type;
};
While I give a +1 to #ipc answer and it is very well for C++11 enabled compilers, for C++03 compilers you should use a different approach, since default value for template arguments of a function is not supported in C++03. So I have this code:
template<class U, class V = void>
struct Get
{
static const char id = 'A';
typedef U type;
};
template< class T >
struct is_type {
static const bool value = true;
};
template<class U>
struct Get<U,
typename std::tr1::enable_if<is_type<typename U::der>::value, void>::type>
{
static const char id = 'B';
typedef typename Get<typename U::der>::type type;
};
Related
I am interested in designing a template interface where the const ness of the function and return type itself changes depending on the template parameter. I have managed to do this for the return type as follows.
template<typename T, bool canChange>
struct Changable{};
template<typename T>
struct Changable<T,true>
{
typedef T type;
};
template<typename T>
struct Changable<T,false>
{
typedef const T type;
};
template<typename T, bool canChange>
struct Data{
typedef typename Changable<T,canChange>::type DataType;
DataType m_data; //< This makes it const/non-const at compile time.
// This function will also make the return type const/non-const
// at compile time.
DataType& GetDataRef(){ return m_data;}
//However, it seems to me that I still need a second function
//with an explicit "const", which I can't seem to avoid.
DataType& GetDataRef()const{return m_data;}
};
Can I somehow avoid having two const/non-const functions here at compile time using some SFINAE magic? std::enable_if would have been ideal here but it seems to me that const is not a type and that approach may not work. Any suggestions?
Here is an example based on inheritance:
#include <type_traits>
#include <iostream>
template<typename T, bool canChange>
struct Changable { using type = const T; };
template<typename T>
struct Changable<T, true> { using type = std::decay_t<T>; };
template<typename, typename, bool>
struct Base;
template<typename D, typename T>
struct Base<D, T, true> {
using DataType = typename Changable<T, true>::type;
DataType& GetDataRef() { std::cout << "non-const" << std::endl; return static_cast<D*>(this)->m_data; }
};
template<typename D, typename T>
struct Base<D, T, false> {
using DataType = typename Changable<T, false>::type;
DataType& GetDataRef() const { std::cout << "const" << std::endl; return static_cast<const D*>(this)->m_data; }
};
template<typename T, bool canChange>
struct Data: Base<Data<T, canChange>, T, canChange> {
friend class Base<Data<T, canChange>, T, canChange>;
typename Base<Data<T, canChange>, T, canChange>::DataType m_data{};
using Base<Data<T, canChange>, T, canChange>::GetDataRef;
};
int main() {
Data<int, true> d1;
Data<int, false> d2;
d1.GetDataRef();
d2.GetDataRef();
}
As requested, Data has only one definition of the GetDataRef method.
Which one is available, the const one or the other one, depends on the value of canChange.
Note the friend declaration. It allows the base class to access to the private data members of Data.
I think I'd approach this using the templates already available in the standard library. It does not require inheritance or any custom classes.
#include <utility>
template<typename T, bool canChange>
struct Data{
using value_type = T;
using cv_type = std::conditional_t<canChange, value_type, std::add_const_t<value_type>>;
using reference = std::add_lvalue_reference_t<cv_type>;
using const_reference = std::add_lvalue_reference_t<std::add_const_t<cv_type>>;
Data(T t) : m_data(std::move(t)) {}
cv_type m_data; //< This makes it const/non-const at compile time.
// This function will also make the return type const/non-const
// at compile time.
reference GetDataRef(){ return m_data;}
//However, it seems to me that I still need a second function
//with an explicit "const", which I can't seem to avoid.
const_reference GetDataRef() const {return m_data;}
};
int main()
{
Data<int, true> d1 { 10 };
d1.m_data = 12;
const Data<int, true>& rd1 = d1;
auto& a = d1.GetDataRef();
auto& b = rd1.GetDataRef();
a = 12; // compiles fine
// b= 12; won't compile
Data<int, false> d2 { 10 };
const Data<int, false>& rd2 = d2;
auto& c = d2.GetDataRef();
auto& d = rd2.GetDataRef();
// c = 12; // won't compile
// d = 12; // won't compile
}
Now to the question:
Can I somehow avoid having two const/non-const functions here at compile time using some SFINAE magic?
You're almost answering your own question here. SFINAE requires that template arguments are considered in immediate context. Which is a complex way of saying that the expression in std::enable_if<> must depend on some template type.
Unhappily, the template type of T is known by the time the function GetDataRef is evaluated, so enable_if won't help us here.
So if we only want one version of GetDataRef we would indeed have to resort to derivation from a template type (the base class would then be evaluated in immediate context of T).
However, there is a problem even then.
consider:
Data<int, true>& x This is a reference to mutable container containing mutable data
const Data<int, true>& y This is a reference to an immutable container containing mutable data
calling x.GetDataRef() ought to return a mutable reference to an int, otherwise we'll confuse our users.
calling y.GetDataRef() should certainly return a const reference to an int, otherwise again, users may be shocked to learn that a member of a const thing is actually mutable.
Maybe something like this can solve the issue:
#include <type_traits>
#include <iostream>
template<typename T, bool canChange>
struct Changable: std::false_type { using type = const T; };
template<typename T>
struct Changable<T, true>: std::true_type { using type = std::decay_t<T>; };
template<typename T, bool canChange>
struct Data {
using DataTraits = Changable<T, canChange>;
private:
template<typename U>
std::enable_if_t<U::value, typename U::type&>
GetDataRefImpl() { std::cout << "non const" << std::endl; return m_data; }
template<typename U>
std::enable_if_t<not U::value, typename U::type&>
GetDataRefImpl() const { std::cout << "const" << std::endl; return m_data; }
public:
typename DataTraits::type m_data{};
typename DataTraits::type& GetDataRef() { return GetDataRefImpl<DataTraits>(); }
typename DataTraits::type& GetDataRef() const { return GetDataRefImpl<DataTraits>(); }
};
int main() {
Data<int, true> d1;
Data<int, false> d2;
d1.GetDataRef();
d2.GetDataRef();
}
The basic idea is to have both the functions exposed by the class, then forward them internally to the same sfinaed one that is const or non-const (this depends on the value of canChange).
As you can see by running the example, the result is:
non const
const
This is true even if both d1 and d2 have been defined as non const.
The std::enable_if turn on the right internal function at compile time.
Note that I've used what the C++14 offers (as an example std::enable_if_t).
The example can be easily converted to a C++11 based on (std::enable_if_t is nothing more than typename std::enable_if<condition, type>::type and so on).
If C++17 is available to you, have a look at is_const, add_const and remove_const.
Together with if constexpr (), a rather elegant solution should be possible.
I have Int2Type specialization
struct A;
struct B;
template<int i> Int2Type;
template<> Int2Type<1> { typedef A type; };
template<> Int2Type<2> { typedef B type; };
Can I build reverse Type2Int specialization automatically? Type2Int<A>::value==1 and so on
Thank you
PS Of course, I can define macro
#define I2T(i, T) template<> Int2Type<i> { typedef T type; }; template<> Type2Int<T> { static const int value = i; };
but I don't want change existing code, may be exists some other way...
update
A, B and others lives in different files
common.h
template<int i> Int2Type;
a.h
struct A;
template<> Int2Type<1> { typedef A type; };
b.h
struct B;
template<> Int2Type<2> { typedef B type; };
I need two compile-time "maps" - type by int and int by type;
Int2Type<1>::type a;
someFunc(Type2Int<A>::value)
Directly? No. Such a thing would be impossible in C++. But, with an intermediate type, we could implement such a thing ourselves with a few helpers.
Without C++11, check out the Boost.MPL library. Specifically, we want boost::mpl::vector:
typedef boost::mpl::vector<A, B> IndexedTypes;
template <int I>
struct Int2Type {
typedef typename boost::mpl::at<IndexedTypes,
boost::mpl::int_<I - 1>
>::type type;
};
template <typename T>
struct Type2Int {
typedef typename boost::mpl::begin<IndexedTypes>::type begin;
typedef typename boost::mpl::find<IndexedTypes, T>::type iter;
static const int value = boost::mpl::distance<begin, iter>::type::value + 1;
};
That should give you Int2Type<1>::type as A, and Type2Int<B> as 2.
With C++11, we can write these as short metafunctions based on a variadic sequence:
template <typename...> struct sequence { };
using IndexedTypes = sequence<A, B>;
can you re-arrange your code to be more like this?
struct A;
struct B;
template<int I, class Type>
struct Int2Type
{
static const int value = I;
typedef Type type;
};
using type1 = Int2Type<1, A>;
using type2 = Int2Type<2, B>;
This works for me:
#include <iostream>
int getNextInt()
{
static int next = 0;
return ++next;
}
template <typename T> struct TypeToInt
{
static const int value;
};
template <typename T> const int TypeToInt<T>::value = getNextInt();
struct A;
struct B;
struct C;
int main()
{
std::cout << TypeToInt<A>::value << std::endl;
std::cout << TypeToInt<B>::value << std::endl;
std::cout << TypeToInt<C>::value << std::endl;
}
Output:
1
2
3
First off my use case, as I may think in the wrong direction: I want to create a map that maps a value to types. So for example:
Map<std::string> map;
map.insert<int, double, char>("Hey");
auto string = map.at<int, double, char>();
This alone is fairly easy to do with std::type_index. However, I want to add the possibility to match types that are not exact the searched ones, when they are convertible. So the following should also return "Hey", as float can be converted to double:
auto string = map.at<int, float, char>();
I can't use type_index for this case as std::is_convertible only works directly on types. This would be the version without conversion, but as far as it seems it's not easily possible to add conversion handling into it without major changes.
My current attempt looks kind of like the following, please note that this is not working and just shows what I have tried to implement:
template<typename T>
class Map {
T value;
std::vector<Map<T>> children; // all the children of the current node.
// in the above example, if this was
// the int node, the only child
// would be the double node
template<typename T1>
constexpr bool is_convertible() const {
return std::is_convertible<__T__, T1>::value; // this isn't applicable
// since __T__ can't be
// stored (this nodes
// type)
}
public:
template<typename T1, typename... Tn>
void insert(T&& value) {
// iterate through/create the child nodes until the last template param
}
template<typename T1, typename... Tn>
T& at() {
// iterate through thechild nodes until a matching child is found
// either exact match or a convertible
for(auto &c: children) {
// if the above function would work
if(c.template is_convertible<T1>()) {
return c.template at<Tn...>();
}
}
}
}
Now I'm at my wits end how to achieve this. I thought of implementing lambdas as comparator functions, but while the lambda can store the type of the current node, it can't accept a template parameter on call to compare to.
Is there some C+1y generic lambda comparator magic, or even an easier way?
I hope this does what you want, there's ample space for extension and for creating template specialization that attach to any type combination you want. It's not super-pretty, but it can probably be refactored a bit and beautified.
#include <iostream>
template <typename... Args>
struct map {
};
template <>
struct map<int, float, char> {
static constexpr char value[] = "int float char";
};
constexpr char map<int,float,char>::value[];
template <typename T>
struct map<int, T> {
static constexpr typename std::enable_if<std::is_integral<T>::value, char>::type value[] = "int, T";
};
template <typename T>
constexpr typename std::enable_if<std::is_integral<T>::value, char>::type map<int,T>::value[];
int main() {
std::string v = map<int,float,char>::value;
std::string w = map<int,int>::value;
std::string w2 = map<int,unsigned>::value;
// std::string w3 = map<int,float>::value; Won't compile
std::cout << v << "\n";
std::cout << w << "\n";
std::cout << w2 << "\n";
return 0;
}
I wrote some weird code using boost::fusion that comes close to doing what you want:
#include <boost/fusion/container/map.hpp>
#include <boost/fusion/include/insert.hpp>
#include <boost/fusion/include/pair.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <string>
#include <iostream>
#include <tuple>
#include <type_traits>
#include <memory>
template <std::size_t Value1, std::size_t Value2>
struct MinSizeT {
static const std::size_t value = (Value1 > Value2) ? Value2 : Value1;
};
template<typename T1, typename T2, std::size_t N>
struct TupleIsConvertibleHelper {
static const bool value = std::is_convertible<typename std::tuple_element<N - 1, T1>::type, typename std::tuple_element<N - 1, T2>::type>::value && TupleIsConvertibleHelper<T1, T2, N - 1>::value;
};
template<typename T1, typename T2>
struct TupleIsConvertibleHelper<T1, T2, 0> {
static const bool value = true;
};
template<typename T1, typename T2>
bool TupleIsConvertible() { // Return true if all types in T1 are convertible to their corresponding type in T2
if (std::tuple_size<T1>::value != std::tuple_size<T2>::value)
return false;
constexpr std::size_t minSize = MinSizeT<std::tuple_size<T1>::value, std::tuple_size<T2>::value>::value;
return TupleIsConvertibleHelper<T1, T2, minSize>::value;
}
template<typename MapInserter>
class Map {
MapInserter mc;
template<typename... Types>
struct do_at {
template <typename T>
void operator()(T const& x) const { // Find an exact match or the last convertible match
typedef std::tuple<Types...> t1;
typedef typename T::first_type t2;
if (exactMatch)
return;
if (std::is_same<t1, t2>::value) {
exactMatch = true;
value = x.second;
}
else if (TupleIsConvertible<t1, t2>())
value = x.second;
}
mutable bool exactMatch;
mutable typename MapInserter::value_type value;
do_at() : exactMatch(false) {}
};
public:
Map(MapInserter _mc) : mc(_mc) { }
template<typename... Types>
typename MapInserter::value_type at() {
do_at<Types...> res;
boost::fusion::for_each(mc.data->map, res);
return res.value;
}
};
template<typename ValueType, typename MapType = boost::fusion::map<>, typename ParentType = void*>
struct MapInserter {
typedef ValueType value_type;
struct Helper {
MapType map;
std::shared_ptr<ParentType> parent; // Must keep parent alive because fusion is lazy.
Helper() = default;
Helper(MapType&& _map, std::shared_ptr<ParentType> _parent) : map(std::move(_map)), parent(_parent) {}
};
std::shared_ptr<Helper> data;
template<typename... KeyTypes>
auto Insert(ValueType value) -> MapInserter<ValueType, decltype(boost::fusion::insert(data->map, boost::fusion::end(data->map), boost::fusion::make_pair<std::tuple<KeyTypes...>>(value))), Helper> {
auto newMap = boost::fusion::insert(data->map, boost::fusion::end(data->map), boost::fusion::make_pair<std::tuple<KeyTypes...>>(value));
return MapInserter<ValueType, decltype(newMap), Helper>(std::move(newMap), data);
}
MapInserter() : data(std::make_shared<Helper>()) { }
MapInserter(MapType&& _map, std::shared_ptr<ParentType> _parent) : data(std::make_shared<Helper>(std::move(_map), _parent)) {}
MapInserter(MapInserter&&) = default;
MapInserter(const MapInserter&) = default;
};
int main() {
auto mc = MapInserter<std::string>().
Insert<int, char, float>("***int, char, float***").
Insert<float, double>("***float, double***").
Insert<int>("***int***").
Insert<unsigned, bool>("***unsigned, bool***");
Map<decltype(mc)> map(mc);
std::cout << map.at<int, char, float>() << std::endl; // "***int, char, float***"
std::cout << map.at<int, char, double>() << std::endl; // "***int, char, float***"
std::cout << map.at<char>() << std::endl; // "***int***"
return 0;
}
template<class...>struct types { typedef types type; };
template<class T, class types>struct type_index;
template<class T, class...Ts>
struct type_index<T,types<T, Ts...>>:
std::integral_constant<unsigned,0>
{};
template<class T, class T0, class...Ts>
struct type_index<T,types<T0, Ts...>>:
std::integral_constant<unsigned,type_index<T,types<Ts...>::value+1>
{};
template<template<class>class filter, class types_in, class types_out=types<>, class details=void>
struct filter;
template<template<class>class filter, class T0, class... Ts, class... Zs>
struct filter<filter, types<T0,types...>, types<Zs...>,
typename std::enable_if< filter<T0>::value >::type
>: filter<filter, types<types...>, types<Zs...,T0>>
{};
template<template<class>class filter, class T0, class... Ts, class... Zs>
struct filter<filter, types<T0,types...>, types<Zs...>,
typename std::enable_if< !filter<T0>::value >::type
>: filter<filter, types<types...>, types<Zs...>>
{};
template<template<class>class filter, class... Zs>
struct filter<filter, types<>, types<Zs...>,
void
>: types<Zs...>
{};
template<typename T>
struct convertable_to_test {
template<typename U>
using test = std::is_convertible<U, T>;
};
template<class T, class types>
struct get_convertable_to_types:filter< convertable_to_test<T>::template test, types> {};
which is a start.
Create a master types<Ts...> of all of the types your system supports. Call this SupportedTypes.
Map types<Ts...> to std::vector<unsigned> of each type offset in the above list. Now you can store a collection of types at runtime. Call this a runtime type vector.
When adding an entry types<Args...> to the map, run get_convertable_to_types on each type in types<Args...>, and build a cross product in types< types<...>... >. Store the resulting exponential number of runtime type vectors in your implementation details map.
When you query with types<Ts...>, conver to the runtime type vector, and look it up in the implementation details map. And done!
An alternative approach would be to write get_convertable_from_types, and do the mapping to an exponential number of types<Ts...> at the query point, convert each to a runtime type vector. When adding stuff to the map, store only one runtime type vector. This has slower lookup performance, but faster setup performance, and uses far less memory.
I was going to finish this, but got busy.
I would like to specialize a function template such that the return type changes depending on the type of the template argument.
class ReturnTypeSpecialization
{
public:
template<typename T>
T Item();
};
// Normally just return the template type
template<typename T>
T ReturnTypeSpecialization::Item() { ... }
// When a float is specified, return an int
// This doesn't work:
template<float>
int ReturnTypeSpecialization::Item() { ... }
Is this possible? I can't use C++11.
Since the specialization has to agree with the base template on the return type, you can make it so by adding a "return type trait", a struct you can specialize and draw the true return type from:
// in the normal case, just the identity
template<class T>
struct item_return{ typedef T type; };
template<class T>
typename item_return<T>::type item();
template<>
struct item_return<float>{ typedef int type; };
template<>
int item<float>();
Live example.
Note that you might want to stick to the following, so you only need to update the return-type in the item_return specialization.
template<>
item_return<float>::type foo<float>(){ ... }
// note: No `typename` needed, because `float` is not a dependent type
Do all of the specialization in a worker class and use a simple function as a wrapper that will be specialized implicitly.
#include <iostream>
using std::cout;
// worker class -- return a reference to the given value
template< typename V > struct worker
{
typedef V const & type;
static type get( V const & v ) { return v; }
};
// worker class specialization -- convert 'unsigned char' to 'int'
template<> struct worker<unsigned char>
{
typedef int type;
static type get( unsigned char const & v ) { return v; }
};
// mapper function
template< typename V > typename worker<V>::type mapper( V const & v )
{
return worker<V>::get(v);
}
int main()
{
char a='A';
unsigned char b='B';
cout << "a=" << mapper(a) << ", b=" << mapper(b) << "\n";
}
In this example, the specialization of unsigned char causes it to be converted to an int so that cout will display it as a number instead of as a character, generating the following output...
a=A, b=66
Perhaps you could use the following hack. Given these simple type traits:
template<bool b, typename T, typename U>
struct conditional { typedef T type; };
template<typename T, typename U>
struct conditional<false, T, U> { typedef U type; };
template<typename T, typename U>
struct is_same { static const bool value = false; };
template<typename T>
struct is_same<T, T> { static const bool value = true; };
You could write your class and specialized member function as follows:
class ReturnTypeSpecialization
{
public:
template<typename T>
typename conditional<is_same<T, float>::value, int, T>::type
Item();
};
// Normally just return the template type
template<typename T>
typename conditional<is_same<T, float>::value, int, T>::type
ReturnTypeSpecialization::Item() { return T(); }
// When a float is specified, return an int
template<>
int ReturnTypeSpecialization::Item<float>() { return 1.0f; }
Simple test program (uses C++11 just for verification):
int main()
{
ReturnTypeSpecialization obj;
static_assert(std::is_same<decltype(obj.Item<bool>()), bool>::value, "!");
static_assert(std::is_same<decltype(obj.Item<float>()), int>::value, "!");
}
Here is a live example.
You can do template specializations like so:
template<typename T>
T item() {
return T();
}
template<>
float item<float>() {
return 1.0f;
}
Hi I tried to use the template specialization for returning the parameter value for primitives as well as std::string data, while doing so I was getting lot of unresolved external, redefinition kind of errors.
so if any one face something like this, he/she can use something like below when want to return different data types including string,
NOTE: both the Template function must be the part of the Header file (*.h)...
so we are using template specialization string data type here...
inside class as a inline member we have to use template specialize method and in the same file we can define the template as well.
class ConfigFileParser
{
public:
bool ParseConfigFile(const std::string& file_name);
template <typename T>
T GetParameterValue(const std::string key);
template <>
std::string GetParameterValue<std::string>(const std::string key)
{
std::string param_val = "";
//do logical operation here...
return param_val;
}
private:
// private functions...
// private data...
};
template <typename T>
T ConfigFileParser::GetParameterValue(const std::string key)
{
T param_val = 0;
std::stringstream ss;
std::string val_str;
// do some operation here...
ss << val_str.c_str();
ss >> param_val;
return param_val;
}
I want to define an operator<< for all enums, to cout the value and print that it is an enum like this:
code:
enum AnyEnum{A,B,C};
AnyEnum enm = A;
cout << enm <<endl;
output:
This is an enum which has a value equal to 0
I know a way of doing this with Boost library by using is_enum struct. But I don’t understand how it works. So that's why, in general, I am interested how to identify if the veriable is a class type, union type or an enum (in compile time).
Determining class types you could use the fact that member pointers exist
template<typename A, typename B>
struct issame { };
template<typename A>
struct issame<A, A> { typedef void type; };
template<typename> struct tovoid { typedef void type; };
template<typename T, typename = void>
struct isclass { static bool const value = false; };
template<typename C>
struct isclass<C, typename tovoid<int C::*>::type> {
static bool const value = true;
};
You cannot detect the difference of an union and a non-union class. At least I don't know how, and boost doesn't know either.
I think detecting enums could work by making sure T isn't a class, function or integral type, and then trying to assign to an integral type. You could
template<typename E, typename = void>
struct isenum {
struct No { char x; };
struct Yes { No n1; No n2; };
struct nullsink {};
static No checkI(nullsink*); // accept null pointer constants
static Yes checkI(...);
static Yes checkE(int);
static No checkE(...);
static bool const value = (sizeof(checkI(E())) == sizeof(Yes)) &&
(sizeof(checkE(E())) == sizeof(Yes));
};
// class
template<typename E>
struct isenum<E, typename tovoid<int E::*>::type> {
static bool const value = false;
};
// reference
template<typename R>
struct isenum<R&, void> {
static bool const value = false;
};
// function (FuntionType() will error out).
template<typename F>
struct isenum<F, typename issame<void(F), void(F*)>::type> {
static bool const value = false;
};
// array (ArrayType() will error out)
template<typename E>
struct isenum<E[], void> {
static bool const value = false;
};
template<typename E, int N>
struct isenum<E[N], void> {
static bool const value = false;
};
Quick & dirty test (works on GCC/clang/comeau):
enum A { };
struct B { };
typedef int &C;
typedef void D();
typedef int E;
typedef long F;
typedef int const G;
typedef int H[1];
template<typename T, bool E>
struct confirm { typedef char x[(T::value == E) ? 1 : -1]; };
int main() {
confirm< isenum<A>, true >();
confirm< isenum<B>, false >();
confirm< isenum<C>, false >();
confirm< isenum<D>, false >();
confirm< isenum<E>, false >();
confirm< isenum<F>, false >();
confirm< isenum<G>, false >();
confirm< isenum<H>, false >();
}
I am interested how to identify if the veriable is a class type, union type or an enum (in compile time).
boost::type_traits
Even C++ TR1 has got a <type_traits> header to support that functionality. In C++0x everything's gonna be a lot better.
For example the following machinery makes use of SFINAE to check whether the argument passed is a class type:
template<typename T>struct Check_If_T_Is_Class_Type
{
template<typename C> static char func (char C::*p);
template<typename C> static long func (...);
enum{val = CHECKER(func,Check_If_T_Is_Class_Type)};
};
The MACRO CHECKER is
#define CHECKER(func_name,class_name) \
sizeof(class_name<T>::template func_name<T>(0)) == 1
To understand how type_traits work you need to have some basic knowledge of templates including template metaprogramming and SFINAE.
This is usually done with compiler hooks. The compiler has special functions that "fill" the template with the apropriate value (at least in C++0x where type_traits has been standardized).
For instance the is_pod trait uses the __is_pod compiler hook under VC 10 to get the apropriate information.
its not possible to know the variable type at compile time.