Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the 'Declare before use' rule not required inside a class? [duplicate]

I'm wondering why the declare-before-use rule of C++ doesn't hold inside a class.

Look at this example:

#ifdef BASE
struct Base {
#endif
    struct B;
    struct A {
        B *b;
        A(){ b->foo(); }
    };

    struct B {
        void foo() {}
    };
#ifdef BASE
};
#endif

int main( ) { return 0; }

If BASE is defined, the code is valid.

Within A's constructor I can use B::foo, which hasn't been declared yet.

Why does this work and, mostly, why only works inside a class?

like image 957
peoro Avatar asked Oct 30 '10 20:10

peoro


People also ask

Why Forward declaration is necessary?

Forward declaration is used in languages that require declaration before use; it is necessary for mutual recursion in such languages, as it is impossible to define such functions (or data structures) without a forward reference in one definition: one of the functions (respectively, data structures) must be defined ...

Can a class be declared inside a function?

A class declared inside a function becomes local to that function and is called Local Class in C++. A local class name can only be used locally i.e., inside the function and not outside it. The methods of a local class must be defined inside it only.

How do you declare a class without definition?

It is possible to declare a class without defining it (forward declaration) as long as it is defined later on within the translation unit. In the case of functions, one can declare a function without defining it within the translation unit, and the linker will link it to its definition in a different translation unit.


2 Answers

Well, to be pedantic there's no "declare before use rule" in C++. There are rules of name lookup, which are pretty complicated, but which can be (and often are) roughly simplified into the generic "declare before use rule" with a number of exceptions. (In a way, the situation is similar to "operator precedence and associativity" rules. While the language specification has no such concepts, we often use them in practice, even though they are not entirely accurate.)

This is actually one of those exceptions. Member function definitions in C++ are specifically and intentionally excluded from that "declare before use rule" in a sense that name lookup from the bodies of these members is performed as if they are defined after the class definition.

The language specification states that in 3.4.1/8 (and footnote 30), although it uses a different wording. It says that during the name lookup from the member function definition, the entire class definition is inspected, not just the portion above the member function definition. Footnote 30 additionally states though that the lookup rules are the same for functions defined inside the class definition or outside the class definition (which is pretty much what I said above).

Your example is a bit non-trivial. It raises the immediate question about member function definitions in nested classes: should they be interpreted as if they are defined after the definition of the most enclosing class? The answer is yes. 3.4.1/8 covers this situation as well.

"Design & Evolution of C++" book describes the reasoning behind these decisions.

like image 101
AnT Avatar answered Oct 26 '22 11:10

AnT


That's because member functions are compiled only after the whole class definition has been parsed by the compiler, even when the function definition is written inline, whereas regular functions are compiled immediatedly after being read. The C++ standard requires this behaviour.

like image 31
Sven Marnach Avatar answered Oct 26 '22 11:10

Sven Marnach