Weird use of std :: map constructor
I was looking for an implementation of std :: map runtime ordering and found this solution: STL std :: map dynamic ordering
This is clear to me, but I don't understand how the OrderingType can be used in the std :: map constructor. std :: map has a constructor that takes a comparator object as an argument. So from my point of view it is ok to use the code:
int main()
{
Ordering<int> test_ordering( ASCENDING );
CUSTOMMAP map1( test_ordering );
return 0;
}
But the code from the above topic also compiles:
int main()
{
CUSTOMMAP map1( ASCENDING );
//...
return 0;
}
I don't understand why this works: the std :: map constructor should not receive an OrderingType enumeration argument instead of the Order object itself.
source to share
If the constructor on Ordering<int>
that accepts your enum is not declared as explicit
, then it is considered a "conversion constructor" that can be automatically inserted when the compiler needs to convert from your enum type to a type Ordering<int>
. So the compiler effectively accepts this:
CUSTOMMAP map1( ASCENDING );
and converting it to this:
CUSTOMMAP map1( Ordering<int>(ASCENDING) );
This is called implicit conversion .
source to share