我在这里问了一个问题C++ template argument with expression,得到了答案,但现在我想转到这样的事情:
#include <complex>
#include <type_traits>
using namespace std;
template<class T, class U>
complex< conditional<(sizeof( T ) > sizeof( U )), T, U>::type > my_method(T x, U y) { }
这给了我错误:
main.cpp:10:65: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp> struct std::complex’
main.cpp:10:65: error: expected a type, got ‘std::conditional<(sizeof (T) > sizeof (U)), int, float>::type’
我尝试在T和U之前添加关键字typename和class,但这也改变了错误:
main.cpp:37:1: error: wrong number of template arguments (1, should be 3)
/usr/include/c++/4.7/type_traits:77:12: error: provided for ‘template<bool <anonymous>, class, class> struct std::conditional’
main.cpp:10:10: error: template argument 1 is invalid
main.cpp:10:1: error: expected unqualified-id at end of input
我使用g ++和-std = c ++ 11作为linux上的编译器选项。反正有没有做我想做的事情?
感谢。
答案 0 :(得分:2)
正如您的编译器所说:
main.cpp:10:65: error: expected a type, got ‘std::conditional<(sizeof (T) > sizeof (U)), int, float>::type’
std::conditional<...
之前缺少一个类型名称。