Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leaving member functions undefined

Tags:

c++

Is it legal in C++ to leave certain member functions of a class undefined, and still use the class, except for these functions? For example:

// A.hpp
struct A {
    void f();
    void g();
};

// A.cpp
#include "A.h"
void A::f() { }

// A_g.cpp
#include "A.h"
void A::g() { }

// main.cpp
#include "A.h"
int main() {
    A* a = new A;
    a->f();
}

A::g() would be defined in a separate A_g.cpp file that gets compiled&linked only for certain build settings.

In the real code these are large parts of code with additional dependencies to external libraries that are only needed for certain build products.

Are there limits in how A can be used, if not all of its member functions are defined?

like image 880
tmlen Avatar asked Jan 14 '20 10:01

tmlen


People also ask

Do member functions need to be defined?

Although member functions can be defined either inside a class declaration or separately, no member functions can be added to a class after the class is defined. Classes containing member functions can have many declarations, but the member functions themselves must have only one definition in a program.

Which function is a non-member function of a class?

Non-member Function: The function which is declared outside the class is known as the non-member function of that class. Below is the difference between the two: The member function can appear outside of the class body (for instance, in the implementation file).

When would you use a non-member function?

Use a nonmember function if you don't need type conversion in the first argument or don't need access to private data. Use a member function if you do need access to private data. And then each user can decide how to call it.


2 Answers

A function is only required to be defined if it is odr-used.

One and only one definition of every non-inline function or variable that is odr-used (see below) is required to appear in the entire program (including any standard and user-defined libraries). The compiler is not required to diagnose this violation, but the behavior of the program that violates it is undefined.

4) Functions are ODR-used if

  • A function whose name appears as a potentially-evaluated expression (including named function, overloaded operator, user-defined conversion, user-defined placement forms of operator new, non-default initialization) is odr-used if it is selected by overload resolution, except when it is an unqualified pure virtual member function or a pointer-to-member to a pure virtual function (since C++17).
  • virtual member function is odr-used if it is not a pure virtual member function (addresses of virtual member functions are required to construct the vtable)
  • An allocation or deallocation function for a class is odr-used by a new expression appearing in a potentially-evaluated expression
  • A deallocation function for a class is odr-used by a delete expression appearing in a potentially-evaluated expression
  • A non-placement allocation or deallocation function for a class is odr-used by the definition of a constructor of that class.
  • A non-placement deallocation function for a class is odr-used by the definition of the destructor of that class, or by being selected by the lookup at the point of definition of a virtual destructor
  • An assignment operator in a class T that is a member or base of another class U is odr-used by an implicitly-defined copy-assignment or move-assignment functions of U.
  • A constructor (including default constructors) for a class is odr-used by the initialization that selects it.
  • A destructor for a class is odr-used if it is potentially invoked

https://en.cppreference.com/w/cpp/language/definition

like image 61
VLL Avatar answered Sep 22 '22 02:09

VLL


To restate your question: Is the following complete program legal?

struct A {
    void f() {};
    void g();  // Declared but not defined (and not used).
};

int main() {
    A a;
    a.f();
}

Yes

I am using n4296 which is the C++14 draft, but this has not changed over the years.

9.3 p4 [class.mfct] says:

There shall be at most one definition of a non-inline member function in a program; no diagnostic is required.

Note the "at most" - thus zero definitions are allowed.

like image 32
Martin Bonner supports Monica Avatar answered Sep 23 '22 02:09

Martin Bonner supports Monica