fekir
fekir

Reputation: 101

Do two classes declarations differing from a friend class make an ODR violation?

Consider a.hpp

class foo{
  int c;
};

and b.hpp

class bar;
class foo{
  friend bar;

  // from here identical to a.hpp
  int c;
};

Is it, strictly speaking, an ODR-violation?

Upvotes: 1

Views: 80

Answers (1)

KamilCuk
KamilCuk

Reputation: 141145

Yes. ODR is clear, from cppreference (emphasis mine):

There can be more than one definition in a program of each of the following: class type, [...], as long as all of the following is true:

  • [... some other points...]
  • each definition consists of the same sequence of tokens [...]
  • [...]

The first token in class foo definition in b.hpp the "friend" token differs from "int" token inside class foo definition in a.hpp - just this is enough. Note how restrictive that point is - it talks about tokens, not even about meaning of these tokens (but note there are also other points with further restrictions). If these two header files are meant to be used in two translation units that are going to be linked together, ODR is violated.

Upvotes: 5

Related Questions