C++宏中的模板?

use*_*738 5 c++ macros templates

是否可以这样做

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

template<typename T2> void somefun() {
    ... 
    abc<int>(...);
    abc<double>(...);
    ...
}
Run Code Online (Sandbox Code Playgroud)

我每次打电话给abc都不写

Fle*_*exo 10

在C++ 11中,您可以:

template<typename T2> void somefun() {
    template <typename T>
    using abc = __abc<T, T2>;
}
Run Code Online (Sandbox Code Playgroud)

没有它你可以使用宏,但你需要做:

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

//usage:

abc(Type) instance;
Run Code Online (Sandbox Code Playgroud)

但由于这看起来不太自然,我会亲自避免它.

如果你想避免宏前C++ 11,你可以做类似的事情:

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();
}
Run Code Online (Sandbox Code Playgroud)

但诚实地说,即使是宏观案例也不那么可读

(注:__abc保留)