'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'沒有多次定義。它只是在其各自的頭文件中聲明。 –