std::make_pair vs C++11 uniform initializer

11,995

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.

Share:
11,995
rubenvb
Author by

rubenvb

Be sure to check out my github, and ResearchGate profiles which show most of what I do that might be professionally relevant.

Updated on June 20, 2022

Comments

  • rubenvb
    rubenvb almost 2 years

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

    Thanks!

    • Motti
      Motti about 13 years
      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).
  • rubenvb
    rubenvb about 13 years
    The world disagrees with you: stackoverflow.com/questions/3250123/… 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...
  • Johannes Schaub - litb
    Johannes Schaub - litb about 13 years
    @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<T>), which isn't really senseful for pairs.
  • rubenvb
    rubenvb about 13 years
    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)
  • Johannes Schaub - litb
    Johannes Schaub - litb about 13 years
    @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.
  • UncleBens
    UncleBens about 13 years
    @rubenvb: I don't think it is make_pair that works around anything. It's the constructor of pair: std::pair<const int, int> p = std::pair<int, int>(1, 2);