Why can a class that does not implement member functions be instantiated in C++?

Why can a class that does not implement member functions be instantiated in C++?


14

For example:

class B;

class A {
public:
    B f();
};

int main(int, char**) {
    A a; // no exception and error

    return 0;
}

The class A may be an incomplete type.

Why can class A be instantiated in this example?

I know that the program will fail to compile when the member function f is called in the code.

Share
Improve this question

1

  • Try looking up virtual and pure virtual as well as the keyword static

    – A P

    19 hours ago

2 Answers
2

Reset to default


19

In C++ entities don’t need to be defined in the same translation unit in which they are used (exceptions apply). So it not being defined in one translation unit doesn’t mean that you won’t define and explicitly instantiate the member function in another translation unit.

Also, it is not necessary to define entities that are not ODR-used (e.g. called) at all in C++.

This is exactly the same for free functions. You also only need to define a free function if you actually ODR-use (e.g. call) it and the definition needs to be only in one translation unit.

The class A may be an incomplete type.

A is complete after the class A { /*...*/ }; definition. Whether member functions are defined doesn’t affect completeness.

I know that the program will fail to compile when the member function f is called in the code.

But it will fail only in the linker step when all translation units are linked together, because before that the compiler can’t know that the function isn’t defined and instantiated somewhere else. If the function isn’t ODR-used (e.g. called), then the linker has no reason to look for a definition for it anyway.


Note that B being incomplete is also not a problem. Return types need to be complete only in the definition of a function, not its declaration, and you can have B be completed before you define the member function in another translation unit.


ODR in the above means "one definition rule". This is the rule that there shall be exactly one definition of a given entity in the whole program. The rule doesn’t apply in full to all entities and in particular templated entities can be defined once in each translation as long as the definitions are identical (exact rules are more complicated).

"ODR-use" refers to a use of an entity that would trigger the one definition rule to require (at least) one definition of the entity to exist somewhere in the program. These are for example calling a function or taking the address of a function.

See https://en.cppreference.com/w/cpp/language/definition for details.

Share
Improve this answer

2

  • remind me again please, what is ODR?

    – kesarling He-Him

    19 hours ago

  • @kesarlingHe-Him One definition rule.

    – Jason Liam

    19 hours ago



0

Why can class A be instantiated in this example?

Because when defining a non-static local variable like A a; the requirement is that the defined type is a complete type and since A is complete at the point of the definition A a;, it satisfied that requirement.


Also note that B f(); is a declaration and not a definition, and when declaring a function/member function, the return type can be of incomplete type like B.

Share
Improve this answer

4

  • Giving a concept a name doesn't really explain it. The question seems to be asking, why isn't a definition required for A::f in order for the type to be "complete"?

    – Karl Knechtel

    19 hours ago

  • @KarlKnechtel That is what the last statement of my answer answers. In particular, B f(); is a declaration and there is no requirement in this declaration for B to a complete type.

    – Jason Liam

    19 hours ago


  • But again, the question isn't what the requirements are. The question is the justification for the requirements – consequentially, not deontologically.

    – Karl Knechtel

    19 hours ago

  • @KarlKnechtel I read the question as "how is this allowed" rather that "why is this allowed" because OP wrote in the question "The class A may be an incomplete type." which seems to suggest that they are not aware of whether A is complete or not etc.

    – Jason Liam

    19 hours ago



Your Answer


Post as a guest

Required, but never shown


By clicking тАЬPost Your AnswerтАЭ, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged

or ask your own question.

Leave a Reply

Your email address will not be published. Required fields are marked *