Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template in a Macros in C++?

Is it possible to do so

# define abc<T1> __abc<T1, T2>

template<typename T2> void somefun() {
    ... 
    abc<int>(...);
    abc<double>(...);
    ...
}

Just to not write it every time i call abc

like image 659
user1386738 Avatar asked May 10 '12 09:05

user1386738


1 Answers

In C++11 you can do:

template<typename T2> void somefun() {
    template <typename T>
    using abc = __abc<T, T2>;
}

Without that you can use a macro but you'd need to do:

#define abc(T1) __abc<T1, T2>

//usage:

abc(Type) instance;

but since that doesn't look very natural I'd avoid it personally.

If you want to avoid the macro pre-C++11 you can do something like:

template <typename T2>
struct type {
  template <typename T1>
  struct lookup {
    typedef __abc<T1,T2> type;
  };
};

template <typename T2> void somefun() {
  typedef type<T2> abc;
  typename abc::template lookup<int>::type();
}

But in all honesty that's less readable than even the macro case

(Note: __abc is reserved)

like image 183
Flexo Avatar answered Oct 03 '22 16:10

Flexo