假设您有以下 C++ 类的定义:
class A {
// Methods
#ifdef X
// Hidden methods in some translation units
#endif
};
Run Code Online (Sandbox Code Playgroud)
这是否违反了班级的一个定义规则?有哪些相关危害?我怀疑如果使用成员函数指针或虚函数,这很可能会中断。否则使用安全吗?
我在 Objective C++ 的上下文中考虑它。头文件包含在纯 C++ 和 Objective C++ 翻译单元中。我的想法是使用 OBJC 宏保护具有 Objective-C 类型的方法。否则,我必须对标头中的所有 Objective-C 类型使用 void 指针,但这样我就失去了强类型,并且必须在整个代码中添加丑陋的静态强制转换。
c++ objective-c conditional-compilation objective-c++ one-definition-rule
以下是示例代码:
using namespace std;
struct A {
A(unique_ptr<int> s)
: _s(move(s)){}
unique_ptr<int> _s;
};
int main(int argc, const char * argv[]) {
auto&& ptr = make_unique<int>(5);
A a{ptr}; // Error
A b{move(ptr)}; // OK
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的第一个猜测是它应该在不使用'move'的情况下工作,但我在clang上得到'调用隐式删除的复制构造函数'.也许'ptr'的类型不是rvalue(奇怪的是?)?有人可以澄清一下吗?