编译这个:
template < class T, class Y, class ...Args >
struct isSame
{
static constexpr bool value = std::conditional<
sizeof...( Args ),
typename std::conditional<
std::is_same< T, Y >::value,
isSame< Y, Args... >, // Error!
std::false_type >::type,
std::is_same< T, Y > >::type::value;
};
int main()
{
qDebug() << isSame< double, int >::value;
return EXIT_SUCCESS;
}
给我这个编译器错误:
error: wrong number of template arguments (1, should be 2 or more)
问题是isSame< double, int >
有一个空的Args
参数包,所以isSame< Y, Args... >
实际上变成isSame< Y >
这与签名不匹配。
但我的问题是:为什么要评估该分支?sizeof...( Args )
is false
,所以内std:conditional
不应该被评估。这不是运行时代码,而是编译器knows that sizeof..( Args )
will never be true
与给定的模板类型。
如果你好奇的话,它应该是一个可变版本std::is_same
,并不是说它有效...