获取 c 数组上的 begin 返回类型
Get the return type of begin on a c array
我想以通用方式获取std::begin
的返回类型。我目前的解决方案是:
using type = decltype(std::begin(std::declval<T>()));
它在T = std::vector<int>
时有效.但我不明白为什么以下内容不起作用:
using type = decltype(std::begin(std::declval<int[3]>()));
我收到错误:
example.cpp:83:60: error: no matching function for call to ‘begin(int [3])’
using type = decltype(std::begin(std::declval<int[3]>()));
如何以通用方式获取std::begin
的返回类型?
数组
的重载为:
template< class T, std::size_t N >
constexpr T* begin( T (&array)[N] );
std::declval<int[3]>()
给你一个int(&&)[3]
,这与那个重载不匹配。它也与正常的容器过载不匹配,因为这些是SFINAE-ed c.begin()
。因此,您没有匹配的功能。
相反,您需要的是将数组的左值引用传递给begin()
,以使迭代器返回。因此,在使用别名时,您需要手动提供该 lvalue 引用:
template <class T>
using type = decltype(std::begin(std::declval<T>()));
using arr = type<int(&)[3]>; // int*
或者让别名本身为您提供左值引用:
template <class T>
using type = decltype(std::begin(std::declval<T&>()));
using arr = type<int[3]>; // int*
前者对我来说似乎更正确,但是 YMMV。
相关文章:
- 如何获取std::result_of函数的返回类型
- 奇怪的结构&GCC&clang(void*返回类型)
- 如何建立使用模板函数的lambda函数的尾部返回类型
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 函数作为模板参数,是否对返回类型强制约束
- C++中函数的向量返回类型引发错误
- 检查函数返回类型是否与STL容器类型值相同
- 为什么返回类型中需要typename?C++
- <Windows>为什么 std::thread::native_handle 返回类型为"long long unsigned int"的值,而不是 void*(又名 HANDLE)?
- 警告:在函数返回类型 [-Wignore 限定符] 时忽略类型限定符
- 为什么 c++(g++) 不允许模板返回类型和函数名称之间有空格?
- 为什么返回类型的'const'限定符对标有 __forceinline/内联的函数没有影响?
- 推导 std::vector::back() 的返回类型
- 在 c++ 中将函数返回类型指定为模板参数
- 使用 SWIG 更改生成的 CS 函数中的返回类型
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- 获取 c 数组上的 begin 返回类型
- 容器中的 begin() 和 end() 方法应始终具有相同的返回类型