9

考虑以下:

struct foo {
};

struct bar {
};

int main()
{
    foo f;
    bar b;
    std::variant<foo*, bool> v;
    v = &b; // compiles in Visual Studio 19 v16.7.3
}

正如评论中所讨论的,我相信以上是合法的 C++17。有一个提案P0608R3被接受为解决这种令人惊讶的行为的标准,但它在 2018 年(在圣地亚哥会议上)被接受,因此适用于 C++20 而不是 C++17。此外,P0608R3 当前未在 Visual Studio 中实现,即使编译为 C++20 预览版也是如此。

从指向非 foo 的指针创建此变体的最佳/最简洁的方法是编译时错误?我相信以下内容有效,但如果变体包含多个项目,则有很多样板。

struct foo {
};

struct bar {
};

using variant_type = std::variant<foo*, bool>;
struct var_wrapper : public variant_type
{
    var_wrapper(foo* v = nullptr) : variant_type(v)
    {}

    var_wrapper(bool v) : variant_type(v)
    {}

    template<typename T>
    var_wrapper(T*) = delete;
};

int main()
{
    foo f;
    bar b;

    var_wrapper vw;
    vw = &f; // fine
    vw = true; // fine
    vw = &b; // compile time error
}

我错过了一些更简单的方法吗?

4

2 回答 2

2

Another solution is to introduce another bool wrapper that doesn't construct from anything except from bool:

#include <variant>

struct foo {};
struct bar {};

struct StrongBool {
    bool value = false;

    StrongBool() noexcept = default;
    StrongBool(bool b) noexcept : value(b) {}

    template<class T>
    StrongBool(T) = delete;
};

int main() {
    foo f;
    bar b;
    std::variant<foo*, StrongBool> v;
    v = true;
    v = &f;
    v = &b; // fails to compile
} 

Regardless, limiting acceptable initializers requires introducing type wrappers with user-defined constructors.

于 2020-10-08T14:32:31.673 回答
0

I think the cleanest way to handle implicit conversion doing surprising things with respect to variants is to use a "strong variant" type if the behavior of std::variant is a problem; i.e., implement a variant type that enforces construction only using types that are exactly the types in the variant.

It is easy to test if a type is a member of a parameter pack in C++17 using std::disjunction, leading to an implementation as below:

template<typename... Ts>
class strong_variant : public std::variant<Ts...> 
{
public:
    template <typename T, typename U = 
        typename std::enable_if<std::disjunction_v<std::is_same<T, Ts>...>>::type>
    strong_variant(T v) : std::variant<Ts...>(v)
    {}

    strong_variant() : std::variant<Ts...>()
    {}
};

struct foo {};
struct bar {};

int main()
{
    foo f;
    bar b;
    const foo c_f;

    strong_variant<foo*, std::string, bool> sv;

    sv = &f; // okay.
    sv = true; // okay.
    sv = "foo"s; // okay.

    sv = "foo"; //no, must a string.
    sv = &b;  // no, must be a foo.
    sv = &c_f; // no, must be non-const.
}
于 2020-10-10T16:25:45.303 回答