Search code examples
c++multiset

CPP Primer 5ed. 15.8.1 multiset related


While reading Cpp Primer 5ed Chapter15.8.1 (page631), I am confused by the definition of private member - item.

The original goes like:

std::multiset<std::shared_ptr<Quote>, decltype(compare)*> items{compare};

Shouldn't this be like:

std::multiset<std::shared_ptr<Quote>, decltype(compare)*> items(compare);

Here the compare works as the constructor argument.


Solution

  • Either works.

    Since C++11, it's valid to specify an initializer, including cases that call a constructor, using {}.

    One difference is that the form using {} causes overload resolution to prefer initializer_list constructors, but as long as compare can't possibly convert to std::shared_ptr<Quote>, that's not an issue here.