何时使用`static_assert`而不是SFINAE?

时间:2012-08-16 09:58:10

标签: c++ templates c++11 sfinae static-assert

我一直在使用(并使用过)static_assert来标记模板参数值的不需要的值。但是,对于我遇到的所有情况,通过SFINAE禁用这些不需要的值似乎更好,更优雅。例如

template<typename T,
         class = std::enable_if<std::is_floating_point<T>::value>::type>
struct Foo { ... };

而不是

template<typename T>
struct Foo {
  static_assert(std::is_floating_point<T>::value,
                "Foo<T>: T must be floating point :-(");
  ...
};

所以我的问题是:何时使用static_assert而不是SFINAE?为什么?

修改

我认为到目前为止我学到的是以下内容

1 SFINAE是一个多功能且功能强大但可能非常复杂的工具,可用于许多任务,包括功能重载解析(有些人认为这是其唯一目的)。

2 SFINAE可以以相对简单的方式使用static_assert,但它出现在声明(类或函数)中而不是它的定义(或者可以在类前向声明​​中插入static_assert?)。这使得更加逐字,更清晰的代码。但是,由于SFINAE很复杂,它往往比简单的static_assert更难做到。

3 另一方面,static_assert具有更清晰的编译器错误消息,有些人似乎认为这两者都是主要目的。

4 个答案:

答案 0 :(得分:12)

如果你想要使用另一个重载,你可以使用SFINAE;如果它们都不适合这样的参数,你可以使用static_assert

答案 1 :(得分:10)

static_assert使编译失败。 SFINAE允许您删除一个可能的过载。

答案 2 :(得分:7)

如果您想强制static_assert是浮点类型,我认为T是正确的选择。此方法比SFINAE解决方案更清楚地表明您的意图。

答案 3 :(得分:6)

首先,使用SFINAE可能导致选择另一个过载,原先是一个更糟糕的匹配并且不会被考虑。

在有其他重载的情况下,但是没有超载是可行的,你会得到一些像这样的好东西:

#include <type_traits>

void f(int){}
void f(bool){}
void f(char){}
void f(float){}
void f(long){}
void f(double){}
void f(short){}
void f(unsigned){}
void f(void*){}
void f(void (*)()){}

template<class C, class T = int>
using EnableIf = typename std::enable_if<C::value, T>::type;

template<class T>
struct sfinae_false : std::false_type{};

template<class T> 
void f(T&&, EnableIf<sfinae_false<T>> = 0){}

int main(){ struct X{}; f(X()); }

输出:

source.cpp: In function 'int main()':
source.cpp:23:30: error: no matching function for call to 'f(main()::X)'
source.cpp:23:30: note: candidates are:
source.cpp:3:6: note: void f(int)
source.cpp:3:6: note:   no known conversion for argument 1 from 'main()::X' to 'int'
source.cpp:4:6: note: void f(bool)
source.cpp:4:6: note:   no known conversion for argument 1 from 'main()::X' to 'bool'
source.cpp:5:6: note: void f(char)
source.cpp:5:6: note:   no known conversion for argument 1 from 'main()::X' to 'char'
source.cpp:6:6: note: void f(float)
source.cpp:6:6: note:   no known conversion for argument 1 from 'main()::X' to 'float'
source.cpp:7:6: note: void f(long int)
source.cpp:7:6: note:   no known conversion for argument 1 from 'main()::X' to 'long int'
source.cpp:8:6: note: void f(double)
source.cpp:8:6: note:   no known conversion for argument 1 from 'main()::X' to 'double'
source.cpp:9:6: note: void f(short int)
source.cpp:9:6: note:   no known conversion for argument 1 from 'main()::X' to 'short int'
source.cpp:10:6: note: void f(unsigned int)
source.cpp:10:6: note:   no known conversion for argument 1 from 'main()::X' to 'unsigned int'
source.cpp:11:6: note: void f(void*)
source.cpp:11:6: note:   no known conversion for argument 1 from 'main()::X' to 'void*'
source.cpp:12:6: note: void f(void (*)())
source.cpp:12:6: note:   no known conversion for argument 1 from 'main()::X' to 'void (*)()'
source.cpp:21:6: note: template<class T> void f(T&&, EnableIf<sfinae_false<T> >)
source.cpp:21:6: note:   template argument deduction/substitution failed: