1
我有下面的代碼可以編譯和行爲預期:constexpr構造對象與私有成員
struct A
{
//private:
int v;
friend constexpr A f(int v);
};
constexpr A f(int v)
{
return A{v};
}
constexpr A operator""_A(unsigned long long v)
{
return f(static_cast<int>(v));
}
我可以構建A
類型的對象是這樣的:
auto a = 1_A;
如果我做值A::v
私有我編譯錯誤
No matching constructor for initialization of 'A'
如果我添加const ructor到A
,我得到一個不同的copilation錯誤:
Constexpr function's return type 'A' is not a literal type
有沒有一種方法,使A::v
私有,仍然有constexpr
函數來構造A
類型的對象?
你能展示你添加的ctor嗎?它是「constexpr」嗎? – Angew
@Angew不,它不是......謝謝:)這是問題 - 從constexpr中調用非constexpr函數... – Felics