Search code examples
c++11constructorinitializer-listconstexpruser-defined-literals

How do I require a type go through a user defined literal?


I have a POD-type Foo that I want to require users instantiate through my user defined literal (default copying, moving, and assignment are OK):

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)};
}

As an example, I want something like this:

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
}

However with the above code I get the following compilation error (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

How do I reopen the {}-ctor for business, but only for my user defined literal?


Solution

  • You do not have a two-parameter constructor at the moment. Just write one:

    struct Foo
    {
    private:
        const char* var1;
        std::size_t var2;
    
        friend constexpr Foo operator"" _foo (const char* v, std::size_t len);
    
        constexpr Foo(const char* param1, std::size_t param2) : var1(param1), var2(param2) {}
    };