[英]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 我每次打电话给abc都不写
In C++11 you can do: 在C ++ 11中,您可以:
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: 如果你想避免宏前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();
}
But in all honesty that's less readable than even the macro case 但诚实地说,即使是宏观案例也不那么可读
(Note: __abc
is reserved) (注意:保留__abc
)
Yes, but you need to use round parentheses. 是的,但你需要使用圆括号。
# define abc(T1) __abc<T1, T2>
template<typename T2> void somefun() {
...
abc(int)(...);
abc(double)(...);
}
Edit: My recommendation is not using macros for this kind of abbreviation at all. 编辑:我建议根本不使用这种缩写的宏。 Use awoodlands solution or maybe a default template parameter. 使用awoodlands解决方案或可能是默认模板参数。 And thou shall not use reserved names. 你不应该使用保留的名字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.