std::piecewise_construct
,在 constexpr
。我想知道在 header 中使用 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)
};
}
翻译单元 1
#include "a.hpp"
翻译单元 2
#include "a.hpp"
TU 1 中 f
中的 std::piecewise_construct
指的对象与 TU 2 中 f
中的对象不同。我怀疑 f
违反 ODR。
N3290(也可能是 ISO/IEC 14882:2011)在 3.2/5 中表示以下情况是 ODR 的异常(exception):
a name can refer to a const object with internal or no linkage if the object has the same literal type in all definitions of D, and the object is initialized with a constant expression (5.19), and the value (but not the address) of the object is used, and the object has the same value in all definitions of D;
f
几乎满足所有要求,但是“使用对象的值(而不是地址)”对我来说似乎模棱两可。 std::piecewise_construct_t
确实没有状态,但是调用 std::pair
的分段构造函数涉及到调用 隐式声明的复制构造函数>std::piecewise_construct_t
,其参数是 const std::piecewise_construct_t &
。地址被“使用”了,不是吗?
我很困惑。
最佳答案
您似乎已经在该提升邮件列表发布中找到了答案。是的,在我看来,这是未定义的行为,或者至少没有足够明确的定义行为。
见 this usenet discussion对于正在讨论的同一问题。
关于c++ - std::piecewise_construct 不会导致 ODR 违规吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7634419/