The boost.variant library and boost::make_recursive_variant

Discussion in 'C++' started by =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 5, 2007.

  1. We have this code:
    typedef boost::make_recursive_variant<
    int
    , std::vector< boost::recursive_variant_ >
    >::type int_tree_t;

    Use of the resultant variant type is as expected:
    std::vector< int_tree_t > subresult;
    subresult.push_back(3);
    subresult.push_back(5);

    std::vector< int_tree_t > result;
    result.push_back(1);
    result.push_back(subresult);
    result.push_back(7);

    int_tree_t var(result);
    According to documentation in library of boost.variant, a variable var
    it's ( 1 ( 3 5 ) 7 ).
    Queastion:Who do it print in language C++ by next of std::cout?
    Thanks in advance.
     
    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 5, 2007
    #1
    1. Advertising

  2. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    dasjotre Guest

    On 5 Jul, 02:49, Miros aw Makowiecki <>
    wrote:
    > We have this code:
    > typedef boost::make_recursive_variant<
    > int
    > , std::vector< boost::recursive_variant_ >
    > >::type int_tree_t;

    > Use of the resultant variant type is as expected:
    > std::vector< int_tree_t > subresult;
    > subresult.push_back(3);
    > subresult.push_back(5);
    >
    > std::vector< int_tree_t > result;
    > result.push_back(1);
    > result.push_back(subresult);
    > result.push_back(7);
    >
    > int_tree_t var(result);
    > According to documentation in library of boost.variant, a variable var
    > it's ( 1 ( 3 5 ) 7 ).
    > Queastion:Who do it print in language C++ by next of std::cout?
    > Thanks in advance.


    On 5 Jul, 02:49, Miros aw Makowiecki <>
    wrote:
    > We have this code:
    > typedef boost::make_recursive_variant<
    > int
    > , std::vector< boost::recursive_variant_ >
    > >::type int_tree_t;

    > Use of the resultant variant type is as expected:
    > std::vector< int_tree_t > subresult;
    > subresult.push_back(3);
    > subresult.push_back(5);
    >
    > std::vector< int_tree_t > result;
    > result.push_back(1);
    > result.push_back(subresult);
    > result.push_back(7);
    >
    > int_tree_t var(result);
    > According to documentation in library of boost.variant, a variable var
    > it's ( 1 ( 3 5 ) 7 ).
    > Queastion:Who do it print in language C++ by next of std::cout?
    > Thanks in advance.


    (I suppose the question is HOW to print var to get ( 1 ( 3 5 ) 7 ) )

    you provide visitor with function operator overloaded for all
    the types your variant can hold.

    struct print_visitor : public boost::static_visitor<void>
    {
    // print int
    void operator()(int i) const
    {
    std::cout << i << ' ';
    }
    // print vector of int_tree_t
    void operator()(std::vector<int_tree_t> const & v) const
    {
    std::cout << " ( ";
    for(std::size_t i=0; i<v.size(); ++i )
    boost::apply_visitor(print_visitor(), v);

    std::cout << " ) ";
    }
    };

    boost::apply_visitor(print_visitor(), var);

    regards

    DS
     
    dasjotre, Jul 6, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cr88192
    Replies:
    3
    Views:
    586
    cr88192
    Sep 7, 2005
  2. romi
    Replies:
    1
    Views:
    2,624
    Alf P. Steinbach
    Sep 2, 2004
  3. romi
    Replies:
    2
    Views:
    665
    Joona I Palaste
    Sep 2, 2004
  4. morten
    Replies:
    1
    Views:
    589
    Thomas J. Gritzan
    May 10, 2007
  5. Replies:
    5
    Views:
    382
    Miroslaw Makowiecki
    Sep 29, 2007
Loading...

Share This Page