Std :: make_pair vs c ++ 0x uniform initializer

Is there a drawback to using the latter? Is std::make_pair more versatile / compatible or is it interchangeable?

Thanks!

+4
source share
1 answer

How are they connected? Using the initializer list constructor does not work for a pair, since a pair is heterogeneously typed, while the initializer list constructor uses initializer_list<T> , which can only be used to get a uniformly typed list of initializers.

(Looking at the specification, it really should be called “initializer list constructor”, not “initializer list constructor.” Do you really mean the link to the first? If not, what do you relate to?).

If you just reference the initialization of std::pair<> using a list of initializers, using std::make_pair and using auto , I think both of them are ok.

 auto p = std::make_pair(a, b); std::pair<A, B> p{a, b}; 

If you already have types A and B and you can use them for pair , then the list of initializers is a good way to use. If you haven’t done this then make_pair might be good. If you have types A and B , but are not sure whether they were converted correctly (i.e. they should not be arrays or function types, and you probably also want them to be non-reference types), then this it may be easier to use std::make_pair , which will decompose the type of expressions correctly.

+4
source

Source: https://habr.com/ru/post/1343541/


All Articles