samedi 23 mai 2020

C ++ associated container custom type is initialized in the class

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?

Aucun commentaire:

Enregistrer un commentaire