Constructing a value through two implicit constructors?

前端 未结 1 1334
清酒与你
清酒与你 2021-01-14 14:08

TLDR: I have two templatized classes Outer and Inner. Inner can be implicitly constructed from X

相关标签:
1条回答
  • 2021-01-14 15:02

    Your first example requires two user defined conversions to compile — SomeType -> Inner -> Outer. However, at most one user defined conversion can be applied implicitly.

    Quoting N3337, §12.3 [class.conv]

    1   Type conversions of class objects can be specified by constructors and by conversion functions. These conversions are called user-defined conversions and are used for implicit type conversions (Clause 4), for initialization (8.5), and for explicit type conversions (5.4, 5.2.9).

    4   At most one user-defined conversion (constructor or conversion function) is implicitly applied to a single value.


    If the goal is to avoid having to mention Inner<SomeType> in the return statement, you can use list initialization.

    Outer<Inner<SomeType>> DoSomethingWorks2() {
      SomeType value;
      return {std::move(value)};
    }
    
    0 讨论(0)
提交回复
热议问题