A friend sent me this and I really don’t know what it is, inside the loop. Whatever it be it calls the std::set constructor half million times…. any help appreciated. I would expect a compiler error, but it actually compiles in g++ 4.4 and 4.5 and the behavior is different to copy construction…
#include <stdio.h>
#include <stdlib.h>
#include <boost/unordered_map.hpp>
#include <set>
#include <string>
typedef boost::unordered_map<int, std::set<int> > mymap;
int main () {
mymap map;
for ( int i = 0 ; i < 1000 ; i++ )
{
std::set<int> map[i] ;
}
return 1;
};
You are dealing with a GCC-specific non-standard extension of C++ language. Each iteration defines an array of
std::mapobjects withielements (and immediately destroys it).In standard C++ it is illegal to use a non-constant expression to specify array size, so the code is not legal C++. It compiles, again, only because GCC allows it as an extension.