std::begin 的 SFINAE 问题
SFINAE problems with std::begin
以下代码在 Visual Studio 2013 中有效,但无法使用 clang 或 gcc 进行编译,所以我猜它不是有效的 c++。
#include <iterator>
#include <type_traits>
#include <vector>
template< typename T >
class SupportsStdBegin
{
// std::false_type TestHasBegin( ... ) always exists.
template< typename >
static std::false_type TestSupportsBegin( ... );
// std::true_type TestHasBegin( int ) only exists if std::begin( T() ) is a valid expression.
template< typename U >
static auto TestSupportsBegin( int ) -> decltype( std::begin( std::declval< T >() ), std::true_type{ } );
public:
static const bool value = decltype( TestSupportsBegin< T >( 0 ) )::value;
};
static_assert( !SupportsStdBegin< int >::value, "'int' does not support std::begin()." );
const int TestArray[] = { 0, 1, 2, 3, 4, };
static_assert( SupportsStdBegin< decltype( TestArray ) >::value, "'const int TestArray[]' supports std::begin()." );
static_assert( SupportsStdBegin< std::vector< int > >::value, "'std::vector< int >' supports std::begin()." );
有人可以解释为什么 SFINAE 没有像我预期的那样工作吗?
包含 decltype( std::begin( std::declval< T >() )
的行对于 int
类型的 static_assert
失败,错误为:
error: no matching function for call to 'begin(int)'
我认为这意味着 SFINAE 会失败,返回 std::false_type
的实现将用于设置 SupportsStdBegin< T >::value
。
我知道有其他方法可以编写此测试,但我想知道为什么这种特定方法无效。我之前用它成功测试过成员函数是否存在
要使 SFINAE 发生,表达式需要依赖于模板[=21=]直接上下文中的模板参数,即模板参数列表。
简单的解决方法是将 U
默认为 T
并在依赖表达式中使用 U
:
template <typename U=T>
static auto TestSupportsBegin( int )
-> decltype( std::begin( std::declval< U >() ), std::true_type{ } );
以下代码在 Visual Studio 2013 中有效,但无法使用 clang 或 gcc 进行编译,所以我猜它不是有效的 c++。
#include <iterator>
#include <type_traits>
#include <vector>
template< typename T >
class SupportsStdBegin
{
// std::false_type TestHasBegin( ... ) always exists.
template< typename >
static std::false_type TestSupportsBegin( ... );
// std::true_type TestHasBegin( int ) only exists if std::begin( T() ) is a valid expression.
template< typename U >
static auto TestSupportsBegin( int ) -> decltype( std::begin( std::declval< T >() ), std::true_type{ } );
public:
static const bool value = decltype( TestSupportsBegin< T >( 0 ) )::value;
};
static_assert( !SupportsStdBegin< int >::value, "'int' does not support std::begin()." );
const int TestArray[] = { 0, 1, 2, 3, 4, };
static_assert( SupportsStdBegin< decltype( TestArray ) >::value, "'const int TestArray[]' supports std::begin()." );
static_assert( SupportsStdBegin< std::vector< int > >::value, "'std::vector< int >' supports std::begin()." );
有人可以解释为什么 SFINAE 没有像我预期的那样工作吗?
包含 decltype( std::begin( std::declval< T >() )
的行对于 int
类型的 static_assert
失败,错误为:
error: no matching function for call to 'begin(int)'
我认为这意味着 SFINAE 会失败,返回 std::false_type
的实现将用于设置 SupportsStdBegin< T >::value
。
我知道有其他方法可以编写此测试,但我想知道为什么这种特定方法无效。我之前用它成功测试过成员函数是否存在
要使 SFINAE 发生,表达式需要依赖于模板[=21=]直接上下文中的模板参数,即模板参数列表。
简单的解决方法是将 U
默认为 T
并在依赖表达式中使用 U
:
template <typename U=T>
static auto TestSupportsBegin( int )
-> decltype( std::begin( std::declval< U >() ), std::true_type{ } );