在ac数组上获取begin的返回类型

Vin*_*ent 10 c++ templates iterator decltype c++11

我想以std::begin通用的方式获得返回类型.我目前的解决方案是:

using type = decltype(std::begin(std::declval<T>()));
Run Code Online (Sandbox Code Playgroud)

它适用于T = std::vector<int>.但我不明白为什么以下不起作用:

using type = decltype(std::begin(std::declval<int[3]>()));
Run Code Online (Sandbox Code Playgroud)

我收到错误:

example.cpp:83:60: error: no matching function for call to ‘begin(int [3])’
     using type = decltype(std::begin(std::declval<int[3]>()));
Run Code Online (Sandbox Code Playgroud)

如何以std::begin通用方式获取返回类型?

Bar*_*rry 7

数组的重载是:

template< class T, std::size_t N > 
constexpr T* begin( T (&array)[N] );
Run Code Online (Sandbox Code Playgroud)

std::declval<int[3]>()给你一个int(&&)[3],与超载不匹配.它也与正常的容器重载不匹配,因为它们具有SFINAE编辑功能c.begin().所以你没有匹配的功能.

你需要的是将一个左值引用传递给数组begin(),以使迭代器退出.因此,当您使用别名时,您需要手动提供左值引用:

template <class T>
using type = decltype(std::begin(std::declval<T>()));

using arr = type<int(&)[3]>; // int*
Run Code Online (Sandbox Code Playgroud)

或让别名本身为您提供左值参考:

template <class T>
using type = decltype(std::begin(std::declval<T&>()));

using arr = type<int[3]>; // int*
Run Code Online (Sandbox Code Playgroud)

前者对我来说似乎更正确,但是YMMV.