c++ - std::piecewise_construct 不会导致 ODR 违规吗?

标签 c++ c++11 constexpr linkage one-definition-rule

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 &。地址被“使用”了,不是吗?

我很困惑。

引用:http://lists.boost.org/Archives/boost/2007/06/123353.php

最佳答案

您似乎已经在该提升邮件列表发布中找到了答案。是的,在我看来,这是未定义的行为,或者至少没有足够明确的定义行为。

this usenet discussion对于正在讨论的同一问题。

关于c++ - std::piecewise_construct 不会导致 ODR 违规吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7634419/

相关文章:

.net - 如何在Windows中获取登录用户的全名?

java - 谷歌文档如何在不使用 Flash 查看器的情况下显示我的 .PPT 文件?

c++ - Visual C++ 2008 链接(特别是嵌入 list )花费的时间太长

c++ - 静态类成员的初始化。为什么要构造函数?

c++ - static constexpr 成员的统一初始化

c++ - 带有外部静态表的 constexpr 函数

C++ 引用传递

c++ - 在 C++11 中实现线程安全方法的正确方法

c++ - 是否可以在 constexpr 函数中执行 I/O

c++ - 初始化列表中的类 constexpr 表达式