Why can't std::tuple be element-wise constructed with a std::tuple of compatible types?

后端 未结 1 603
清歌不尽
清歌不尽 2021-02-19 22:09

I can\'t initialize std::tuple elements element-wise from a std::tuple of compatible types. Why doesn\'t it work as with boost::tuple?

1条回答
  •  清酒与你
    2021-02-19 22:47

    Overloads (4) and (5) are poorer matches than (3) when passed a tuple& : they are const& and && overloads, while (3) matches exactly through the magic of perfect forwarding.

    (3) is valid because your Foo(U&&) constructor is overly greedy.

    Add SFINAE checks to Foo(U&&) so that it fails to match when it fails to build:

    template {},int>* =nullptr
    >
    Foo(U &&u) : val(std::forward(u)) {}
    

    The rvalue case should, however, work or be ambiguous. Looking at the error log of your live example, the only error I see is with the lvalue one.

    0 讨论(0)
提交回复
热议问题