类 是否允许在一个程序的不同翻译单元中有不同的定义?
Are classes allowed to have different definitions across different translation units in a program?
考虑到 class 在每个翻译单元中最多定义一次,在不同翻译单元中以不同方式定义 class 是否合式?
用例是在没有动态分配的情况下访问实现细节。 C++ 代码将对已由 C 库分配的指针进行操作。
为了举例,请忽略内存泄漏。
common.hpp
#pragma once
namespace Test {
class Impl;
class A {
void *ptr;
A(void *ptr) : ptr(ptr) {}
friend class Impl;
public:
int plus_one();
};
class B {
void *ptr;
B(void *ptr) : ptr(ptr) {}
friend class Impl;
public:
int plus_two();
};
class Factory {
public:
A getA(int val);
B getB(int val);
};
} // namespace Test
A.cpp
#include "common.hpp"
namespace Test {
class Impl {
public:
static int as_int(A *a) { return *static_cast<int *>(a->ptr) + 1; }
};
int A::plus_one() { return Impl{}.as_int(this); }
} // namespace Test
B.cpp
#include "common.hpp"
namespace Test {
class Impl {
public:
static int as_int(B *b) { return *static_cast<int *>(b->ptr) + 2; }
};
int B::plus_two() { return Impl{}.as_int(this); }
} // namespace Test
Factory.cpp
#include "common.hpp"
namespace Test {
class Impl {
public:
static A getA(int val) { return A(new int{val}); }
static B getB(int val) { return B(new int{val}); }
};
A Factory::getA(int val) { return Impl{}.getA(val); }
B Factory::getB(int val) { return Impl{}.getB(val); }
} // namespace Test
main.cpp
#include <iostream>
#include "common.hpp"
int main() {
Test::Factory factory;
std::cout << factory.getA(1).plus_one() << std::endl;
std::cout << factory.getB(1).plus_two() << std::endl;
return 0;
}
输出:
$ g++ A.cpp B.cpp Factory.cpp main.cpp -o test
$ ./test
2
3
不行,同一个class类型不允许有不同的定义。您的程序直接违反了 ODR,并表现出未定义的行为。
[basic.def.odr]
6 There can be more than one definition of a class type, [...] in
a program provided that each definition appears in a different
translation unit, and provided the definitions satisfy the following
requirements. Given such an entity named D defined in more than one
translation unit, then
- each definition of D shall consist of the same sequence of tokens; and
- [...]
[...] If the definitions of D satisfy all these requirements, then the
behavior is as if there were a single definition of D. If the
definitions of D do not satisfy these requirements, then the behavior
is undefined.
你的两个定义在它们的标记序列上已经很明显不同了,因此不支持 ODR 的条件。
考虑到 class 在每个翻译单元中最多定义一次,在不同翻译单元中以不同方式定义 class 是否合式?
用例是在没有动态分配的情况下访问实现细节。 C++ 代码将对已由 C 库分配的指针进行操作。
为了举例,请忽略内存泄漏。
common.hpp
#pragma once
namespace Test {
class Impl;
class A {
void *ptr;
A(void *ptr) : ptr(ptr) {}
friend class Impl;
public:
int plus_one();
};
class B {
void *ptr;
B(void *ptr) : ptr(ptr) {}
friend class Impl;
public:
int plus_two();
};
class Factory {
public:
A getA(int val);
B getB(int val);
};
} // namespace Test
A.cpp
#include "common.hpp"
namespace Test {
class Impl {
public:
static int as_int(A *a) { return *static_cast<int *>(a->ptr) + 1; }
};
int A::plus_one() { return Impl{}.as_int(this); }
} // namespace Test
B.cpp
#include "common.hpp"
namespace Test {
class Impl {
public:
static int as_int(B *b) { return *static_cast<int *>(b->ptr) + 2; }
};
int B::plus_two() { return Impl{}.as_int(this); }
} // namespace Test
Factory.cpp
#include "common.hpp"
namespace Test {
class Impl {
public:
static A getA(int val) { return A(new int{val}); }
static B getB(int val) { return B(new int{val}); }
};
A Factory::getA(int val) { return Impl{}.getA(val); }
B Factory::getB(int val) { return Impl{}.getB(val); }
} // namespace Test
main.cpp
#include <iostream>
#include "common.hpp"
int main() {
Test::Factory factory;
std::cout << factory.getA(1).plus_one() << std::endl;
std::cout << factory.getB(1).plus_two() << std::endl;
return 0;
}
输出:
$ g++ A.cpp B.cpp Factory.cpp main.cpp -o test
$ ./test
2
3
不行,同一个class类型不允许有不同的定义。您的程序直接违反了 ODR,并表现出未定义的行为。
[basic.def.odr]
6 There can be more than one definition of a class type, [...] in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements. Given such an entity named D defined in more than one translation unit, then
- each definition of D shall consist of the same sequence of tokens; and
- [...]
[...] If the definitions of D satisfy all these requirements, then the behavior is as if there were a single definition of D. If the definitions of D do not satisfy these requirements, then the behavior is undefined.
你的两个定义在它们的标记序列上已经很明显不同了,因此不支持 ODR 的条件。