C++ 宏中的模板? [英] Template in a Macros in C++?
本文介绍了C++ 宏中的模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以吗
# define abc<T1> __abc<T1, T2>
template<typename T2> void somefun() {
...
abc<int>(...);
abc<double>(...);
...
}
只是为了不每次打电话给 abc 都写
Just to not write it every time i call abc
推荐答案
在 C++11 中你可以:
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.
如果您想避免 C++11 之前的宏,您可以执行以下操作:
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
(注:__abc
保留)
这篇关于C++ 宏中的模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文