7

Is there a drawback to using the latter? Is std::make_pair more versatile/compatible or are they truly interchangeable?

Thanks!

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • 1
    Changing the question after an answer pointed out you made a mistake in phrasing it is in poor taste. Better to open a new question (this question was originally about `initializer_list` as seen in @litb's answer). – Motti Mar 13 '11 at 18:49

1 Answers1

8

How are they related? Using an initializer list constructor doesn't work for a pair, because a pair is heterogeneously typed, while an initializer list constructor uses an initializer_list<T>, which is only usable to retrieve an homogeneously typed initializer list.

(Looking into the spec, it should really be called "initializer-list constructor", instead of "initializer list constructor". Do you really mean to refer to the first? If not, what do you refer to?).

If you just refer to initialize a std::pair<> using an initializer list against using std::make_pair and using auto, I think both are fine.

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

If you have already the types A and B and can use them for the pair, then the initializer list is a good way to use. If you haven't, then make_pair might be a good thing. If you have types A and B, but aren't sure whether they are already transformed properly (i.e they should not be array or function types, and you probably also want them to be non-reference types), then it might be easier to use std::make_pair, which will properly decay the type of the expressions.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • 1
    The world disagrees with you: http://stackoverflow.com/questions/3250123/what-would-a-stdmap-extended-initializer-list-look-like/3250450#3250450 I asked that question myself a while back, and used the solution succesfully in GCC. If this wasn't allowed, I'd think it would be a grave defect in the standard... – rubenvb Mar 13 '11 at 16:05
  • 1
    @rubenvb, what that code shows is not an "initializer list constructor". It's a "list-initialization". I suspect you could also say "A construction using an initializer list" or something, possibly with an example along that clarifies what you mean. As stated, the question was tending towards `pair(initializer_list)`, which isn't really senseful for pairs. – Johannes Schaub - litb Mar 13 '11 at 16:09
  • 1
    Ok, a more correct term would indeed be "uniform initializer". Will edit. Are there no `const` gotcha's I could have to worry about? (I remember that `std::make_pair` works around a const issue with `std::map`) – rubenvb Mar 13 '11 at 16:12
  • @rubenvb, yes if you want to create a pair of non-const types, then you can use `make_pair` too. Sometimes you might want to make pairs of const elements. – Johannes Schaub - litb Mar 13 '11 at 16:15
  • 1
    @rubenvb: I don't think it is `make_pair` that works around anything. It's the constructor of pair: `std::pair p = std::pair(1, 2);` – UncleBens Mar 13 '11 at 16:44