【发布时间】:2019-10-19 13:51:22
【问题描述】:
考虑以下示例 (https://godbolt.org/z/pSTUZI):
#include <iterator>
#include <type_traits>
template <typename T>
struct falsy : std::false_type {};
template <
typename T,
typename std::enable_if<falsy<T>::value, int>::type = 0>
void f(std::back_insert_iterator<T>) {}
template <typename T>
void f(T) {}
struct S {};
int main() {
S s;
f<S>(s);
}
用 gcc 8.3 或更早版本编译会报错:
In file included from /opt/compiler-explorer/gcc-8.3.0/include/c++/8.3.0/iterator:63,
from <source>:1:
/opt/compiler-explorer/gcc-8.3.0/include/c++/8.3.0/bits/stl_iterator.h: In instantiation of 'class std::back_insert_iterator<S>':
<source>:19:9: recursively required by substitution of 'template<class T, typename std::enable_if<falsy<T>::value, int>::type <anonymous> > void f(std::back_insert_iterator<_Container>) [with T = S; typename std::enable_if<falsy<T>::value, int>::type <anonymous> = <missing>]'
<source>:19:9: required from here
/opt/compiler-explorer/gcc-8.3.0/include/c++/8.3.0/bits/stl_iterator.h:490:7: error: no type named 'value_type' in 'struct S'
operator=(const typename _Container::value_type& __value)
^~~~~~~~
/opt/compiler-explorer/gcc-8.3.0/include/c++/8.3.0/bits/stl_iterator.h:497:7: error: no type named 'value_type' in 'struct S'
operator=(typename _Container::value_type&& __value)
^~~~~~~~
虽然 clang 和 gcc 9 编译它没有任何错误。这个例子是 SFINAE 的正确使用吗?它是 gcc
【问题讨论】: