std::make_unique、匿名命名空间和 ODR

std::make_unique, anonymous namespace and ODR

请考虑以下测试用例(从 LLVM 源减少):

//% cat foo1.cpp

#include <memory>
namespace {
class A {
  int i;
};
}
class G {
  std::unique_ptr<A> foo() const;
};
std::unique_ptr<A> G::foo() const { return std::make_unique<A>(); }

//% cat foo2.cpp

#include <memory>
namespace {
class A {
  bool a;
};
}
class H {
  std::unique_ptr<A> bar() const;
};
std::unique_ptr<A> H::bar() const { return std::make_unique<A>(); }

这是否违反了一个定义规则?

gcc-6目前是这么认为的:

 ~ % g++ -flto -shared -std=c++14 foo1.cpp foo2.cpp

/home/trippels/gcc_test/usr/local/include/c++/6.0.0/tuple:187:72: warning: type ‘struct _Base’ violates one definition rule [-Wodr]
       typedef _Head_base<_Idx, _Head, __empty_not_final<_Head>::value> _Base;
                                                                         ^
/home/trippels/gcc_test/usr/local/include/c++/6.0.0/tuple:187:72: note: a different type is defined in another translation unit
       typedef _Head_base<_Idx, _Head, __empty_not_final<_Head>::value> _Base;
                                                                         ^
/home/trippels/gcc_test/usr/local/include/c++/6.0.0/tuple:147:13: note: the first difference of corresponding definitions is field ‘_M_head_impl’
       _Head _M_head_impl;
              ^
/home/trippels/gcc_test/usr/local/include/c++/6.0.0/tuple:147:13: note: a field of same name but different type is defined in another translation unit
       _Head _M_head_impl;
              ^
foo1.cpp:3:7: note: type ‘struct A’ defined in anonymous namespace can not match type ‘struct A’
 class A {
        ^
foo2.cpp:3:7: note: the incompatible type defined in anonymous namespace in another translation unit
 class A {
        ^
/home/trippels/gcc_test/usr/local/include/c++/6.0.0/tuple:598:40: warning: type ‘struct _Inherited’ violates one definition rule [-Wodr]
       typedef _Tuple_impl<0, _T1, _T2> _Inherited;
                                         ^
/home/trippels/gcc_test/usr/local/include/c++/6.0.0/tuple:598:40: note: a type with the same name but different base type is defined in another translation unit
       typedef _Tuple_impl<0, _T1, _T2> _Inherited;
                                         ^
/home/trippels/gcc_test/usr/local/include/c++/6.0.0/tuple:102:12: note: type ‘struct _Head_base’ defined in anonymous namespace can not match type ‘struct _Head_base’
     struct _Head_base<_Idx, _Head, false>
             ^
/home/trippels/gcc_test/usr/local/include/c++/6.0.0/tuple:102:12: note: the incompatible type defined in anonymous namespace in another translation unit
     struct _Head_base<_Idx, _Head, false>
             ^
/home/trippels/gcc_test/usr/local/include/c++/6.0.0/bits/unique_ptr.h:151:41: warning: type ‘struct element_type’ violates one definition rule [-Wodr]
       typedef _Tp                       element_type;
                                          ^
/home/trippels/gcc_test/usr/local/include/c++/6.0.0/bits/unique_ptr.h:151:41: note: a different type is defined in another translation unit
       typedef _Tp                       element_type;
                                          ^
foo1.cpp:4:7: note: the first difference of corresponding definitions is field ‘i’
   int i;
        ^
foo2.cpp:4:8: note: a field with different name is defined in another translation unit
   bool a;
         ^
/home/trippels/gcc_test/usr/local/include/c++/6.0.0/tuple:598:40: warning: type ‘struct _Inherited’ violates one definition rule [-Wodr]
       typedef _Tuple_impl<0, _T1, _T2> _Inherited;
                                         ^
/home/trippels/gcc_test/usr/local/include/c++/6.0.0/tuple:598:40: note: a type with the same name but different base type is defined in another translation unit
       typedef _Tuple_impl<0, _T1, _T2> _Inherited;
                                         ^
/home/trippels/gcc_test/usr/local/include/c++/6.0.0/tuple:102:12: note: type ‘struct _Head_base’ defined in anonymous namespace can not match type ‘struct _Head_base’
     struct _Head_base<_Idx, _Head, false>
             ^
/home/trippels/gcc_test/usr/local/include/c++/6.0.0/tuple:102:12: note: the incompatible type defined in anonymous namespace in another translation unit
     struct _Head_base<_Idx, _Head, false>
         ^

您对 class 有两种不同的定义。将两者一起编译会导致直接冲突,除非这两者最终位于不同的名称空间中。我想这可能是对的。

您可能想要了解的是这两个实现功能是否相互排斥。可能有两个具有不同定义的不同源文件,但在某些情况下,有两个定义可能有意义(即我在 Windows 和 Linux 上有一个线程 API,我需要基于我的编译设置的两种不同定义)。

这是 GCC 错误(在开发树中只存在了几天)。该问题是由另一个修复程序引起的,该修复程序使 GCC 将隐式 typedef 视为非匿名的,因此外部结构得到了类型合并(不正确)。测试用例现已修复,我很想知道更多可能出现虚假的警告。