Reputation: 165
Two initialization codes for association container objects of custom type comparison first , not in class:
class A
{
public:
int a;
};
bool compare(const A &a1 , const A &a2)
{ return a1.a > a2.a; }
multiset<A , decltype(compare)> ml(compare);
this code is safety but () initialization statement cannot be used in the class
class z
{
public:
bool compare(const A &a1 , const A &a2)
{ return a1.a > a2.a; }
std::multiset<A , decltype(compare)> ml(compare);
};
this code use () to initialize the container object will wrong, only can use {}
std::multiset<A , decltype(compare)> ml{compare};
this code is safety , why?
Upvotes: 0
Views: 30
Reputation: 52317
This is not particular to your compare. While C++11 allows to initialize class members within the definition, you do that either with {}
(brace initializer) or with assignment (equals initializer).
struct A {
int a = 42; // works
int a{42}; // works
int a(42); // unsupported syntax
};
In your example, this would work:
std::multiset<A, decltype(compare)> ml = std::multiset<A, decltype(compare)>(compare);
Upvotes: 1