SFINAE problems with std::begin
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 不能像我期望的那样工作吗?
对于int
类型的static_assert
,包含decltype( std::begin( std::declval< T >() )
行失败,并显示错误:
error: no matching function for call to 'begin(int)'
我认为这意味着 SFINAE 会失败,并且返回 std::false_type
的实现将用于设置SupportsStdBegin< T >::value
。
我知道有其他方法可以编写此测试,但我想知道为什么这种特定方法无效。我之前已经成功地使用它来测试成员函数是否存在。
要使 SFINAE 发生,表达式需要依赖于模板直接上下文中的模板参数,即模板参数列表。
简单的解决方法是默认U
T
并在依赖表达式中使用U
:
template <typename U=T>
static auto TestSupportsBegin( int )
-> decltype( std::begin( std::declval< U >() ), std::true_type{ } );
现场演示
相关文章:
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 为constchar*定义std::begin合法吗
- constexpr begin of a std::array
- std::ranges::begin 和 std::begin 有什么区别?
- 为什么 std::find( s.begin(), s.end(), val ) 比集合 s 的 s.find(val) 慢 1000 倍<int>?
- 递减 std::vector::begin 是否未定义,即使它从未被使用过?
- std::begin-类型特征中未考虑用户定义的重载
- 当从成员类调用封装的std::begin时,程序崩溃
- C++ - 空的 std::list begin() 和 end() 不相等
- 为什么std::begin()和std::end()适用于固定数组,而不适用于动态数组
- C++代码"x.erase(std::remove(x.begin(), x.end(), ), x.end())"是如何工作的?
- 重载 std::begin() 和 std::end() 用于非数组
- 使用 std::set 的 .begin() 和 .end() 函数会产生意想不到的结果
- 为什么 free 函数不能在 C 数组上运行,而 std::begin 在某些情况下可以在 C++14 中运行?
- std::begin() 和 std::end() 不适用于类中的未知长度数组
- 在 std::list 中,std::d istance(it.begin(), std::p rev(it.end()
- 为什么在 std::vector 擦除中需要 begin()
- 将模式包装为 std::begin; 返回 begin(c); 到函数中
- 为什么在这种情况下"std::begin()"总是返回"const_iterator"?