Re:My Most Important C++ Aha Moments (2006)
by MaulingMonkey on Sunday June 26, 2016 @06:31AM
(#11980182)
Structure layout isn't the hard part, it's having the rest of the language cope with the ambiguity of everything.
// Things.h
namespace Foo { class A { C* c; }; }
namespace Bar { class B { C* c; }; }
Are A::c and B::c the same type?
// TU1.cpp
class C {};
Were you right?
// TU2.cpp
namespace Foo { class C {}; }
namespace Bar { class C {}; }
What about now?
// TU3.cpp
class C {};
namespace Foo { class C {}; }
namespace Bar { class C {}; }
This may get you shot, but is legal. What about now?
Ahh - all three are in the same program. Just pretend they #included <c.h>, <foo/c.h>, and <bar/c.h>. Having A::c's type be different in different contexts is a violation of the "One Definition Rule", for which the punishment is undefined behavior.
Although you can still recreate the above scenarios by just #including <Things.h> after <c.h> and company, but you're a bit more likely to have Things.h #include what it needs to clarify the situation. Hopefully. So maybe we can get away with it!
void foo() { D * d; }
Is that a pointer definition or invoking operator* against two globals?
void bar() { E < 42 > e; }
Now you're just fucking with me. Is that some comparison operators or a template?
void baz() { F < (42) > f; }
That didn't clarify anything. Stop it.
void he_comes() { G < (42) > g(); }
What do you mean I just declared a function? Stop it!
Ahh - all three are in the same program. Just pretend they #included <c.h>, <foo/c.h>, and <bar/c.h>. Having A::c's type be different in different contexts is a violation of the "One Definition Rule", for which the punishment is undefined behavior.
Although you can still recreate the above scenarios by just #including <Things.h> after <c.h> and company, but you're a bit more likely to have Things.h #include what it needs to clarify the situation. Hopefully. So maybe we can get away with it!
Is that a pointer definition or invoking operator* against two globals? Now you're just fucking with me. Is that some comparison operators or a template? That didn't clarify anything. Stop it. What do you mean I just declared a function? Stop it! A function accepting a function pointer? Stooooop!