Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

c++ - Current status of std::make_array

What is the current status of std::make_array function proposed here? I cannot find any information about its potential acceptance. According to cppreference.com, it's in the std::experimental namespace. It's not mentioned at all on C++ compiler support nor on Wikipedia-C++17, Wikipedia-C++20, and C++17 Standard draft.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As @DeiDei writes, C++17 includes template argument deduction for classes, so you can now write:

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

and so on. But there are some (somewhat subtle) remaining use cases where make_pair or make_array can be useful, and you can read about them in: Usefulness of std::make_pair and std::make_tuple in C++1z

@Ruslan correctly notes in a comment that the above is mostly useful when the type of the elements is "obvious" to the compiler, or made explicit for each element. If you want to implicitly construct elements of some type, the above looks like;

std::array arr = { MyType{1,2}, MyType{3,4}, MyType{5,6}, MyType{7,8} };

which is too wordy; and that's one of the cases where you fall back on:

std::make_array<MyType>{ {1,2}, {3,4}, {5,6}, {7,8} };

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...