4 ms·
My favourite is: struct Dummy { Dummy(int) {} }; struct World { World(const Dummy &) {} void hello() {} }; int ma
by rom1v 5y ago
My favourite is:
struct Dummy {
Dummy(int) {}
};
struct World {
World(const Dummy &) {}
void hello() {}
};
int main() {
int value = 42;
World world(Dummy(value)); // fails to compile
// World world(Dummy(42)); // works
world.hello();
return 0;
}
- dw-im-here 5y agoBravo Bjarne! You've done it again!
- slavik81 5y agoThis is known as "the most vexing parse". In 99.9% of cases, it's just a surprising compiler error message that you learn to avoid. However, there's a related version of this problem which can lead to accidentally declaring a function prototype rather than acquiring a lock. You'll get compiler warnings from -Wshadow, but the seriousness of the problem might not be obvious. That said, it's a very rare problem these days, due to the introduction of uniform initialization in C++11.
- rocqua 5y agoFor anyone wondering. The issue is that the line: World world(Dummy(value)); Can be interpreted as the following function declaration: World world(Dummy value); As mentioned in child comment. This is sometimes called the most vexing parse: https://en.wikipedia.org/wiki/Most_vexing_parse https://en.wikipedia.org/wiki/Most_vexing_parse It originates because C++ allows superfluous parentheses around function parameter declarations.