[英]C++ Template in macros
我讀過的職位上這個話題。 但是當我嘗試執行此操作時仍然遇到問題。
template<typename T> struct argument_type;
template<typename T, typename U> struct argument_type<T(U)> { typedef U type; };
#define TEST_TYPE2(who) typename animal < argument_type<T(who)>::type >
template<typename T, typename T2>
struct Pig
{
typedef TEST_TYPE2(int) type;
};
我會得到編譯錯誤
warning C4346: 'argument_type<T(int)>::type' : dependent name is not a type
prefix with 'typename' to indicate a type
see reference to class template instantiation 'Pig<T,T2>' being compiled
error C2923: 'animal' : 'argument_type<T(int)>::type' is not a valid template type argument for parameter 'T'
但是,如果我改變
#define TEST_TYPE2(who) typename animal < argument_type<T(who)>::type >
到下面
#define TEST_TYPE2(who) typename argument_type<T(who)>::type
它編譯良好。 在我將其放入<>括號后,似乎編譯器無法識別“ :: type”。
我該怎么做才能使其正常工作?
預期需要使用typename
,但是可以嘗試這種方式。
#define TEST_TYPE2(who) animal < typename argument_type<T(who)>::type >
我想你拿到你的typename
錯了地方; 它必須在argument_type<T(who)>::type
塊之前,而不是animal<...>
塊之前:
template<typename T> struct animal {};
template<typename T> struct argument_type;
template<typename T, typename U> struct argument_type<T(U)> { typedef U type; };
#define TEST_TYPE2(who) animal<typename argument_type<T(who)>::type>
template<typename T, typename T2> struct Pig {
typedef TEST_TYPE2(int) type;
};
int main(void) {
return 0;
} // end main()
以上對我來說編譯很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.