通用转换运算符模板和移动语义:有通用的解决方案吗



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

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

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

最有问题的情况是初始化对象或对对象的右值引用,给定临时或右值引用。函数调用在所有情况下都有效(我认为(,但我觉得它们太冗长了:

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)

所以,我下一个试验转换运算符:

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)

const&重载似乎是可在右值上调用的,这会产生歧义,使带有隐式转换的复制初始化成为唯一的选项。

然而,考虑以下不那么琐碎的类:

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

它根据输入参数是否可以隐式或显式转换为特定类型来提供泛型隐式或隐式构造函数。这样的逻辑并不是那么奇特,例如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)

由于不同的原因,唯一可用的选项是使用隐式转换直接初始化。然而,这正是隐式转换的危险之处。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)

似乎每个用例都有自己的需求,没有一个组合可以在所有情况下都起作用。

更糟糕的是,以上所有结果都是clang 3.3;其他编译器和版本给出的结果略有不同,同样没有通用的解决方案。例如:活生生的例子。

那么:是否有可能实现所需的功能,或者我应该放弃转换运算符,继续使用显式函数调用

不幸的是,C++标准没有任何特殊的规则来解决这种特殊的歧义。问题来自于这样一个事实:您试图重载两个不同的东西:编译器试图转换为的类型;以及您试图从中转换的引用类型。

通过引入代理类,您可以分两步来拆分解析。步骤1:决定它是r值引用、l值引用还是常量l值引用。步骤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); }
};

最新更新