【发布时间】:2013-06-03 07:56:25
【问题描述】:
以下(建议!)语法有什么缺点吗?
template< typename T >
void f() static_assert(std::is_same< T, int >::value)
{ ; }
而不是 SFINAE(看起来像拐杖):
template< typename T, typename = typename std::enable_if< std::is_same< T, int >::value >::type >
void f() { ; }
甚至更糟:
template< typename T >
typename std::enable_if< std::is_same< T, int >::value >::type
f()
{ ; }
其中禁止使用auto推导结果类型。
【问题讨论】:
-
static_assert不是 SFINAE,它是对必须为真的事物的断言。 -
那不是一个有效的 c++11 代码
-
@BЈовић 我知道。这只是一种可能的语法建议。
-
同时,有可能是more concise。
-
@Piotr99,废话,如果新提案在以前无法使用的上下文中使用关键字,这不是问题,因为这不会与现有代码的含义发生冲突或改变.例如,在 C++ 的类成员上使用 C 关键字
static,或者在 C++11 中使用extern进行显式实例化声明,或者在 C++11 中使用using作为类型别名,或者使用auto作为在 C++11 中的类型推导,或在 C++11 中使用inline用于命名空间,或在 C++11 中的 lambda 表达式中使用mutable,或在 C++14 中使用auto用于通用 lambda 等.等等