简体   繁体   English

如何在结构对象C中使用Boost Variant

[英]How to use Boost Variant with struct objects C++

I have two classes and depending on the nature of key , I would like to get the struct value out of the boost::variant . 我有两个类,根据key的性质,我想从boost::variant获取结构值。 The code is listed below. 该代码在下面列出。

#include <iostream>
#include <boost/variant.hpp>

using namespace std;

class A {
    public:
    struct greeting {
        string hello;
};


class B {
    public:
    struct greeting {
        string bye;
    };
};

typedef boost::variant<A::greeting, B::greeting> greet;

greet getG(string key) {
    greet g;
    if (key == "A") {
        g.hello = "MY ENEMY"; // this line doesn't work
    }
    else {
        g.bye = "MY FRIEND"; // nor this line
    }
    return g;
};

int main() {
    A a;
    B b;
    greet h = getG("A");
    A::greeting my = boost::get<A::greeting>(h);
    cout << my.hello << endl;
    return 0;
}

The exact error that I am getting is: error: no member named 'hello' in 'boost::variant<A::greeting, B::greeting, boost::detail::variant::void_, boost::detail::variant::void_, ...>' g.hello = "MY ENEMY"; 我得到的确切错误是: error: no member named 'hello' in 'boost::variant<A::greeting, B::greeting, boost::detail::variant::void_, boost::detail::variant::void_, ...>' g.hello = "MY ENEMY"; and error: no member named 'bye' in 'boost::variant<A::greeting, B::greeting, .../>' g.bye = "MY FRIEND"; error: no member named 'bye' in 'boost::variant<A::greeting, B::greeting, .../>' g.bye = "MY FRIEND";

Any help is appreciated. 任何帮助表示赞赏。

The variant type doesn't have the .hello and .bye members. 变体类型没有.hello.bye成员。 You can access them via a "visitor" function. 您可以通过“访问者”功能访问它们。 But you still have to decide what to do when the visitor is not applied to the right type. 但是,当访问者未应用到正确的类型时,您仍然必须决定要做什么。 I think you are not using Boost.Variant in the way that is intended to be used. 我认为您没有按照预期的方式使用Boost.Variant。 (For example the conditionals don't smell well). (例如,条件句的气味不好闻)。

http://www.boost.org/doc/libs/1_61_0/doc/html/variant/reference.html#variant.concepts.static-visitor http://www.boost.org/doc/libs/1_61_0/doc/html/variant/reference.html#variant.concepts.static-visitor

struct hello_visitor : boost::static_visitor<>{
    string const& msg;
    hello_visitor(string const& msg) : msg(msg){}
    void operator()(A::greeting& t) const{
        t.hello = msg;
    }
    void operator()(B::greeting& t) const{
        // throw? ignore? other?
    }
};

struct bye_visitor : boost::static_visitor<>{
    string const& msg;
    bye_visitor(string const& msg) : msg(msg){}
    void operator()(A::greeting& t) const{
        // throw? ignore? other?
    }
    void operator()(B::greeting& t) const{
        t.bye = msg;
    }
};


greet getG(string key) {
    greet g;
    if (key == "A") { // is "key" handling the type, if so you can delegate this to the library instead of doing this.
        boost::apply_visitor(hello_visitor("MY ENEMY"), g); 
    }
    else {
        boost::apply_visitor(bye_visitor("MY FRIEND"), g); 
    }
    return g;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM