lundi 5 janvier 2015

Defining a std::map with an undefined comparison function compiles and links, which surprises me

In std::map, c++11, a function (pointer) specifying the key comparison function may be supplied as an optional parameter. While fiddling around with some code that does so, I got some results from the compiler that I didn't expect. I tried both g++ and clang++. As they both agree, I'm guessing the compilers are good, and I'm confused.


If I declare a std::map-type object in which the comparison function template parameter is specified, but then don't actually define the comparison function, the code still compiles and links and I see no warnings.


I compiled with -std=c++11 -O0 -Wall -Wextra



#include <map>

typedef std::map<int, int, bool(*)(int, int)> Objs;

int main() {

#if 0
// Compiles and links as expected.
Objs objs{[](int a, int b) {
return a < b;
}};
#endif

#if 1
// FIXME The lambda definition for the key-comparing function is
// missing; why does this compile and link?
Objs objs;
#endif

objs.emplace(99997, 4210124);
(void)objs;
return 0;
}

Aucun commentaire:

Enregistrer un commentaire