std :: make_array的当前状态

Dan*_*ica 10 c++ arrays

这里std::make_array提出的功能的当前状态是什么?我找不到有关其潜在接受度的任何信息.根据cppreference.com,它位于命名空间中.在C++编译器支持Wikipedia-C++ 17,Wikipedia-C++ 20和C++ 17标准草案中都没有提到它.std::experimental

ein*_*ica 11

正如@DeiDei所写,C ++ 17包含了class的模板参数推导,因此您现在可以编写:

std::pair p (foo, bar);
std::array arr = { 1, 2, 3, 4, 5 };
Run Code Online (Sandbox Code Playgroud)

等等。但是,还有一些(有些微妙的)剩余用例在哪里make_pairmake_array可能有用,您可以在以下文章中阅读它们: C ++ 1z中std :: make_pair和std :: make_tuple的有用性


T.C*_*.C. 8

LEWG投票决定在2016年转发C++ 20 的合并文件(这是在C++ 17功能冻结之后).在LWG问题2814的解决之前,LWG审查暂停了作者的请求.


Pio*_*ycz 6

这个答案提供了提案的状态 - 然而 - 在 C++17 中实现起来非常容易 - 至少这部分:

\n\n
\n

[例子:

\n\n
int i = 1; int& ri = i;\nauto a1 = make_array(i, ri);         // a1 is of type array<int, 2>\nauto a2 = make_array(i, ri, 42L);    // a2 is of type array<long, 3>\nauto a3 = make_array<long>(i, ri);   // a3 is of type array<long, 2>\nauto a4 = make_array<long>();        // a4 is of type array<long, 0>\nauto a5 = make_array();              // ill-formed    auto a6 = make_array<double>(1, 2);  // ill-formed: might narrow \xe2\x80\x93end example]\n
Run Code Online (Sandbox Code Playgroud)\n
\n\n

看:

\n\n
template <typename Dest=void, typename ...Arg>\nconstexpr auto make_array(Arg&& ...arg) {\n   if constexpr (std::is_same<void,Dest>::value)\n      return std::array<std::common_type_t<std::decay_t<Arg>...>, sizeof...(Arg)>{{ std::forward<Arg>(arg)... }};\n   else\n      return std::array<Dest, sizeof...(Arg)>{{ std::forward<Arg>(arg)... }};\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

证据:

\n\n
int main() {\n    int i = 1; int& ri = i;\n    auto a1 = make_array(i, ri);         // a1 is of type array<int, 2>\n    std::cout << print<decltype(a1)>().get() << std::endl; \n    auto a2 = make_array(i, ri, 42L);    // a2 is of type array<long, 3>\n    std::cout << print<decltype(a2)>().get() << std::endl;\n    auto a3 = make_array<long>(i, ri);   // a3 is of type array<long, 2>\n    std::cout << print<decltype(a3)>().get() << std::endl;\n    auto a4 = make_array<long>();        // a4 is of type array<long, 0>\n    std::cout << print<decltype(a4)>().get() << std::endl;\n    // auto a5 = make_array();              // ill-formed\n    // auto a6 = make_array<double>(1, 2);  // ill-formed: might narrow\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出:

\n\n
std::__1::array<int, 2ul>\nstd::__1::array<long, 3ul>\nstd::__1::array<long, 2ul>\nstd::__1::array<long, 0ul>\n
Run Code Online (Sandbox Code Playgroud)\n\n

最后一行make_array<double>(1, 2)产生“缩小范围”错误 - 按照提案中的要求。可以通过在实现中添加 static_cast 来“改进”它。

\n\n

在最新的 clang- demo上。

\n