我如何围绕概念和不完整类型的这种限制进行设计?

问题描述

我有这样的事情:

template <typename T>
requires ::std::movable<T> || ::std::copyable<T> || ::std::is_void_v<T>
class ValueWrapper
{
   // varIoUs functions to do stuff with value
};

class Value {
 public:
    Value(Value const &) = delete;
    Value &operator =(Value const &) = delete;
    Value(Value &&other) noexcept : x_{other.x_} { other.x_ = -1; }
    Value &operator =(Value &&other) {
        Value tmp{::std::move(other)};
        x_ = tmp.x_;
        tmp.x_ = -1;
        return *this;
    }

ValueWrapper<Value> do_something() const;  // Generates error related to incomplete type. :-(

 private:
    int x_;
};

当然,它不起作用,因为在编译器看到 do_something 声明时,Value一个不完整的类型,并且无法测试不匹配的类型是否可以移动或可复制,且不作废。

我应该如何围绕这个进行设计?

我可以更改 ValueWrapper 以便单个成员函数需要一些东西,而不是让类需要一些模板参数。但是,这似乎很钝,因为该类的目的是包装可移动或可复制的东西。我可以将 do_something 移出 Value 类并使其成为一个免费函数。但这似乎过于严格,对于任何 Value 类的方法来说可能不是明智之举。

这里是否还有其他没有这些缺点的设计选择?

在我目前关注的特定非抽象案例中,ValueWrapper 恰好类似于 Boost expected,因此用于表示错误或返回值。

编辑:到目前为止,我最喜欢的答案是使用 auto,并且要求函数定义内联出现才能工作。如果你希望函数定义内联,你可以做这堆体操。但是,这真的很奇怪。

#include <concepts>
#include <utility>


template <typename T>
requires ::std::movable<T> || ::std::copyable<T> || ::std::is_void_v<T>
class ValueWrapper
{
   // varIoUs functions to do stuff with value
};

namespace priv_ {
    // We can forward declare a class without its member functions.
    // But we can't forward declare a function without being able to
    // fully name all of its types.
    class Silly;
}

class Value {
 public:
    Value() : x_{-1} { }
    Value(Value const &) = delete;
    Value &operator =(Value const &) = delete;
    Value(Value &&other) noexcept : x_{other.x_} { other.x_ = -1; }
    Value &operator =(Value &&other) {
        Value tmp{::std::move(other)};
        x_ = tmp.x_;
        tmp.x_ = -1;
        return *this;
    }

    // inline here is basically documenting that we intend to give an
    // inline deFinition later. We might want to say what the actual
    // return type is too.
    auto inline do_something() const;

 private:
    int x_;

    // And we can friend a forward declared class so all of its member
    // functions are basically member functions of this class (and
    // hence have unrestricted access to all member functions and
    // variables).
    friend class priv_::Silly;
};

namespace priv_ {
    class Silly {
     public:
        // And finally,Now that the Value type is 'complete',we can
        // use it as a parameter to the `ValueWrapper` template type.
        static ValueWrapper<Value> p_do_something(Value const &v);
    };
}

auto inline Value::do_something() const
{
    // And Now that the declaration for `p_do_something` has been
    // seen,we can call it.
    return priv_::Silly::p_do_something(*this);
}

ValueWrapper<Value> foo()
{
    Value v;
    return v.do_something();
}

解决方法

在具有默认类型的模板方法中转换 do_something () 怎么样?

我的意思是

template <typename U = Value>
ValueWrapper<U> do_something() const;

或者也

template <int...,typename U = Value>
ValueWrapper<U> do_something() const;

如果你想避免 do_something() 可以被称为解释类型。

,

该类型在成员函数定义中是完整的,因此我们可以使用推导出的返回类型:

auto do_something() const {
    return ValueWrapper<Value>();
}