通用转换运算符模板和移动语义:任何通用解决方案?

iav*_*avr 14 c++ implicit-conversion move-semantics explicit-conversion c++11

这是Explicit ref-qualified转换运算符模板的后续操作.我已经尝试了许多不同的选项,我在这里给出了一些结果,试图看看最终是否有任何解决方案.

假设一个类(例如任何一个)需要以方便,安全(无意外)的方式提供转换到任何可能的类型,以保留移动语义.我可以想到四种不同的方式.

struct A
{
    // explicit conversion operators (nice, safe?)
    template<typename T> explicit operator T&&       () &&;
    template<typename T> explicit operator T&        () &;
    template<typename T> explicit operator const T&  () const&;

    // explicit member function (ugly, safe)
    template<typename T> T&&       cast() &&;
    template<typename T> T&        cast() &;
    template<typename T> const T&  cast() const&;
};

// explicit non-member function (ugly, safe)
template<typename T> T&&       cast(A&&);
template<typename T> T&        cast(A&);
template<typename T> const T&  cast(const A&);

struct B
{
    // implicit conversion operators (nice, dangerous)
    template<typename T> operator T&&       () &&;
    template<typename T> operator T&        () &;
    template<typename T> operator const T&  () const&;
};
Run Code Online (Sandbox Code Playgroud)

最有问题的情况是在给定临时或右值引用的情况下初始化对象或对象的右值引用.函数调用适用于所有情况(我认为),但我发现它们太冗长:

A a;
B b;

struct C {};

C member_move = std::move(a).cast<C>();  // U1. (ugly) OK
C member_temp = A{}.cast<C>();           // (same)

C non_member_move(cast<C>(std::move(a)));  // U2. (ugly) OK
C non_member_temp(cast<C>(A{}));           // (same)
Run Code Online (Sandbox Code Playgroud)

那么,我接下来试验转换运算符:

C direct_move_expl(std::move(a));  // 1. call to constructor of C ambiguous
C direct_temp_expl(A{});           // (same)

C direct_move_impl(std::move(b));  // 2. call to constructor of C ambiguous
C direct_temp_impl(B{});           // (same)

C copy_move_expl = std::move(a);  // 3. no viable conversion from A to C
C copy_temp_expl = A{};           // (same)

C copy_move_impl = std::move(b);  // 4. OK
C copy_temp_impl = B{};           // (same)
Run Code Online (Sandbox Code Playgroud)

似乎const&重载在rvalue上是可调用的,这会产生歧义,使复制初始化与隐式转换作为唯一选项.

但是,请考虑以下不太重要的类:

template<typename T>
struct flexi
{
    static constexpr bool all() { return true; }

    template<typename A, typename... B>
    static constexpr bool all(A a, B... b) { return a && all(b...); }

    template<typename... A>
    using convert_only = typename std::enable_if<
        all(std::is_convertible<A, T>{}...),
    int>::type;

    template<typename... A>
    using explicit_only = typename std::enable_if<
        !all(std::is_convertible<A, T>{}...) &&
        all(std::is_constructible<T, A>{}...),
    int>::type;

    template<typename... A, convert_only<A...> = 0>
    flexi(A&&...);

    template<typename... A, explicit_only<A...> = 0>
    explicit flexi(A&&...);
};

using D = flexi<int>;
Run Code Online (Sandbox Code Playgroud)

它提供了通用的隐式或显式构造函数,具体取决于输入参数是否可以隐式或显式转换为某种类型.这种逻辑并不是那种奇特的,例如,某种实现std::tuple可能就是这样.现在,初始化D给出

D direct_move_expl_flexi(std::move(a));  // F1. call to constructor of D ambiguous
D direct_temp_expl_flexi(A{});           // (same)

D direct_move_impl_flexi(std::move(b));  // F2. OK
D direct_temp_impl_flexi(B{});           // (same)

D copy_move_expl_flexi = std::move(a);  // F3. no viable conversion from A to D
D copy_temp_expl_flexi = A{};           // (same)

D copy_move_impl_flexi = std::move(b);  // F4. conversion from B to D ambiguous
D copy_temp_impl_flexi = B{};           // (same)
Run Code Online (Sandbox Code Playgroud)

出于不同的原因,唯一可用的选项是使用隐式转换进行直接初始化.但是,这正是隐式转换危险的地方.b实际上可能包含一个D,它可能是一种容器,但工作组合调用D的构造函数是一个完全匹配,其b行为就像容器的假元素,导致运行时错误或灾难.

最后,让我们尝试初始化一个右值引用:

D&& ref_direct_move_expl_flexi(std::move(a));  // R1. OK
D&& ref_direct_temp_expl_flexi(A{});           // (same)

D&& ref_direct_move_impl_flexi(std::move(b));  // R2. initialization of D&& from B ambiguous
D&& ref_direct_temp_impl_flexi(B{});           // (same)

D&& ref_copy_move_expl_flexi(std::move(a));  // R3. OK
D&& ref_copy_temp_expl_flexi(A{});           // (same)

D&& ref_copy_move_impl_flexi = std::move(b);  // R4. initialization of D&& from B ambiguous
D&& ref_copy_temp_impl_flexi = B{};           // (same)
Run Code Online (Sandbox Code Playgroud)

似乎每个用例都有自己的要求,并且没有可能适用于所有情况的组合.

更糟糕的是,所有上述结果都是铿锵3.3; 其他编译器和版本的结果略有不同,同样没有通用的解决方案.例如:实例.

那么:是否有任何机会可以按预期工作,或者我应该放弃转换运算符并坚持使用显式函数调用?

ale*_*xk7 4

遗憾的是,C++ 标准没有任何特殊规则来解决这种特定的歧义。问题来自于你试图重载两个不同的东西:编译器试图转换成的类型;以及您尝试从中进行转换的参考类型。

通过引入代理类,您可以将解析分为两步。步骤 1:确定它是右值引用、左值引用还是 const 左值引用。步骤 2:转换为任意类型,保留步骤 1 中关于引用类型的决定。这样,您可以将解决方案与cast()函数一起使用,但不必指定类型:

struct A
{
    class A_r_ref
    {
        A* a_;
    public:
        A_r_ref(A* a) : a_(a) {}
        template <typename T> operator T&&() const&&;
    };

    struct A_ref
    {
        A* a_;
    public:
        A_ref(A* a) : a_(a) {}
        template <typename T> operator T&() const&&;
    };

    struct A_const_ref
    {
        const A* a_;
    public:
        A_const_ref(const A* a) : a_(a) {}
        template <typename T> operator const T&() const&&;
    };

    A_r_ref cast() && { return A_r_ref(this); }
    A_ref cast() & { return A_ref(this); }
    A_const_ref cast() const& { return A_const_ref(this); }
};
Run Code Online (Sandbox Code Playgroud)