>我使用 gcc7.1 编译代码,但出现一些编译错误:
错误:"if"之前应为非限定 ID
错误:"否则"之前应为非限定 ID
class Test
{
int a{1};
string b{"test"};
public:
template <int N> auto & get()
{
if constexpr (N==0)
return a;
else
return b;
}
};
namespace std {
template<> struct tuple_size<Test> { static const int value = 2; };
template<size_t N> struct tuple_element<N, Test>
{
if constexpr (N==0) //error: expected unqualified-id before ‘if’
using type = int;
else //error: expected unqualified-id before ‘else’
using type = string;
//using type = conditional_t<N==0, int, string>; //this works
};
}
constexpr if
语句仍然是if
语句,并且只能出现在可以使用if
语句的上下文中。
这不包括类(模板(定义。