Foo
ユーザー定義リテラルを使用してインスタンス化する必要がある POD タイプがあります (デフォルトのコピー、移動、および割り当ては問題ありません)。
struct Foo
{
private:
Foo() = delete;
friend constexpr Foo operator"" _foo (const char* str, std::siz_t n);
const char* var1;
std::size_t var2;
};
constexpr Foo operator"" _foo (const char* str, std::siz_t n)
{
return Foo{str, MyFunc(str, n)};
}
例として、次のようなものが必要です。
int main()
{
Foo myBar = "Hello, world!"_foo; // ctor via user defined literal OK
Foo myBar2 = myBar; // copy-ctor OK
Foo myBar3 = std::move(myBar2); // move-ctor OK
myBar = myBar3; // assignment OK
Foo myBaz{"Hello, world!", 42}; // ERROR: Must go through the user defined literal
Foo myBaz2; // ERROR: Default ctor not allowed
}
ただし、上記のコードでは、次のコンパイル エラーが発生します (Xcode 4.6.3/Clang):
main.cpp:88:12: error: no matching constructor for initialization of '<anonymous>::Foo'
return Foo{str, MyFunc(str, n)};
^ ~~~~~~~~~~~~~~~~~~~~~
main.cpp:60:5: note: candidate constructor not viable: requires 0 arguments, but 2 were provided
Foo() = delete;
^
main.cpp:57:8: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided
struct Foo
^
main.cpp:57:8: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 2 were provided
ビジネス用に {}-ctor を再開するにはどうすればよいですか?ただし、ユーザー定義リテラルに対してのみです。