'std :: piecewise_construct',在<实用程序>中定义,因为它被声明为constexpr而具有内部链接。我想知道在标题中使用'std :: piecewise_construct'是否会违反ODR。例如:不是std :: piecewise_construct导致ODR违规?
// a.hpp
#include <utility>
#include <tuple>
struct point
{
point(int x, int y)
: x(x), y(y)
{}
int x, y;
};
inline std::pair<point, point> f(int x1, int y1, int x2, int y2)
{
return {
std::piecewise_construct,
std::forward_as_tuple(x1, y1), std::forward_as_tuple(x2, y2)
};
}
// translation unit 1
#include "a.hpp"
// translation unit 2
#include "a.hpp"
在TU 1 f的“标准:: piecewise_construct”是指在TU 2“F”不同的对象比我怀疑“F”违反ODR。
N3290(可能ISO/IEC 14882:2011也)表示以下情况下是ODR的一个例外,在3.2/5:
的名称可以指const对象与内部或没有连锁,如果该对象在D的所有定义中都具有相同的文字类型,并且该对象使用常量表达式(5.19)进行初始化,并且使用该对象的值(但不是地址),并且该对象具有相同的值D的定义;
“F”满足几乎所有的要求,但“使用值的对象(而不是地址)”,似乎模棱两可给我。确实'std :: piecewise_construct_t'没有状态,但'std :: pair'的分段构造函数的调用涉及对'std :: piecewise_construct_t'的隐式声明拷贝构造函数的调用,其参数是'const std :: piecewise_construct_t &'。地址是“使用”的,不是吗?
我很困惑。
参考:http://lists.boost.org/Archives/boost/2007/06/123353.php
+1(我认为)对于一个问题,仍然有我没有线索2分钟后 – sehe
这不是与'std :: cout'相同的问题? –
@Kerrek'std :: cout'没有多次定义。它只是在其各自的头文件中声明。 –