为了使我的问题更清楚,请考虑以下用例:
假设有一个包允许在给定平台上进行一组操作,例如在 Windows 上编辑注册表的类。这个包在其他平台上不存在,因为在其他操作系统上没有等效的操作。
为了简单起见,考虑
窗口/Registry.java
package windows;
public class Registry {
static Registry instance = null;
static{
System.out.println("print from static block");
}
private Registry() {
System.out.println("Registry instance created!");
}
public static synchronized Registry getInstance() {
if (null == instance) {
instance = new Registry();
}
return instance;
}
public void foo() {
System.out.println("foo called.");
}
}
Run Code Online (Sandbox Code Playgroud)
以及我将有条件使用注册表的类:main/Main.java
package main;
import windows.Registry;
public class Main {
public static void test1(boolean onWindows) {
if (onWindows) {
Registry instance = Registry.getInstance();
System.out.println("We …Run Code Online (Sandbox Code Playgroud) 考虑a.hpp
class foo{
int c;
};
Run Code Online (Sandbox Code Playgroud)
和b.hpp
class bar;
class foo{
friend bar;
// from here identical to a.hpp
int c;
};
Run Code Online (Sandbox Code Playgroud)
严格来说,这是否违反 ODR?