Questions tagged [apply-visitor]

boost::apply_visitor — Allows compile-time checked type-safe application of the given visitor to the content of the given variant, ensuring that all types are handled by the visitor.

The behavior of apply_visitor is dependent on the number of arguments on which it operates (i.e., other than the visitor). The function behaves as follows:

  • Overloads accepting one operand invoke the unary function call operator of the given visitor on the content of the given variant operand.
  • Overloads accepting two operands invoke the binary function call operator of the given visitor on the content of the given variant operands.
  • The overload accepting only a visitor returns a generic function object that accepts either one or two arguments and invokes apply_visitor using these arguments and visitor, thus behaving as specified above. (This behavior is particularly useful, for example, when one needs to operate on each element of a sequence of variant objects using a standard library algorithm.)
22 questions
7
votes
1 answer

boost::variant visitor chooses the wrong overload

Why does the following print out "A boolean!"? I realise there are some weird conversion going on, since if I explicitly construct a std::string I get the correct behavior. But why does overload resolution choose visitor::operator()(bool) in the…
4
votes
1 answer

Boost variant apply_visitor compilation error

This simple example code for boost::variant and boost::apply_visitor: #include struct ExprFalse; struct ExprTrue; struct ExprMaybe; typedef boost::variant< ExprFalse, ExprTrue, ExprMaybe >…
RandomBits
  • 4,194
  • 1
  • 17
  • 30
3
votes
1 answer

How to modify boost::apply_visitor to return a value?

I am trying to use boost::variant and boost::apply_visitor. This already works except when I try to make the Vistor's functions to return a (boolean) value. I saw a lot of examples on SO doing this but I wasn't able to create a working sample. This…
Semjon Mössinger
  • 1,798
  • 3
  • 22
  • 32
3
votes
1 answer

How to return Template Value from boost::apply_visitor?

The following code correctly spits out the values 999 and "test" to the console but how do I return these values instead? Something like the commented line was my ultimate goal; return the value that I can then assign to an auto variable (since I…
jslmsca
  • 196
  • 1
  • 14
3
votes
1 answer

using apply_visitor to filter from vector of variant

Yesterday I asked this question and "juanchopanza" answered my question, but unfortunately I cant caught one of bounded types. Since using a "visitor" is more robust, I'm also wondering of anyone could give me a solution using "visitor"? I am…
H'H
  • 1,638
  • 1
  • 15
  • 39
3
votes
1 answer

Why is this binary apply_visitor not working?

#include #include "boost/variant/variant.hpp" #include "boost/variant/apply_visitor.hpp" using namespace std; class Base { public: Base(){} ~Base(){} void AddField(int tag, int value){std::cout << "Base::AddField " << tag <<…
B Faley
  • 17,120
  • 43
  • 133
  • 223
3
votes
1 answer

Boost: Why is apply_visitor not working in this code

I am getting the following compiler error: /usr/include/boost/variant/variant.hpp:832:32: error: no match for call to ‘(const StartsWith) (bool&)’ for the following code. Does anybody know why? #include "boost/variant/variant.hpp" #include…
B Faley
  • 17,120
  • 43
  • 133
  • 223
3
votes
1 answer

What's the return type of boost::apply_visitor (delayed version)

In the following code I have stored the result of delayed apply_visitor in an auto variable. What type can I use instead of auto? Is it possible to use std::function? #include "boost/variant/variant.hpp" #include…
B Faley
  • 17,120
  • 43
  • 133
  • 223
2
votes
0 answers

boost::variant move semantics when use boost::apply_visitor

When I tried to build one AST from another (constant folding), I noticed, that there is unnecessary copy-construction at the time of the call the visitor. But the move-construction should be there, if we talk about the C++11/C++1y. Are there any…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
1
vote
0 answers

variant pixel buffer to vector using boost::apply_visitor

I am trying to convert variablepixelbuffer.vbuffer() to a vector of double values using apply_visitor in boost. I came with this piece of code till now : struct GetVector : public boost::static_visitor> { //values will be…
Sarav
  • 139
  • 1
  • 13
1
vote
1 answer

CGAL::intersection error

i am using CGAL to calculate intersections between 3d triangles. I need to verify if the intersections return points or lines or triangles. typedef CGAL::Cartesian tc; typedef tc::Triangle_3 …
1
vote
1 answer

retrieving objects from boost::variant

I tried to ask my question before, but I think the way I ask my question is not proper. so I tried again here: (still I don't know what subject would be appropriate) first I defined typedef boost::variant vec_variant; typedef…
H'H
  • 1,638
  • 1
  • 15
  • 39
1
vote
2 answers

Is the boost::variant visitor class a requirement?

Am I required to use a visitor class such as class Visitor : public boost::static_visitor<> with boost::variant? If not, are there reasons not to use a visitor? Are there reasons to prefer a visitor class? I ask this question because a visitor…
Mushy
  • 2,535
  • 10
  • 33
  • 54
0
votes
1 answer

why the below boost variant visitor code doesnt work

I have a struct A: struct A { //some implementation } My boost variants are: boost::variant v1 = 1.0; boost::variant v2 = 2.0; My visitor functor is defined as: class SomeWork: public…
Xiyang Liu
  • 81
  • 5
0
votes
1 answer

Is it possible to create Boost multi_index MEM_FUN key extractors for a container of Boost variant?

I am attempting to implement a multi_index container of Boost::variant objects. The variant consists of two derived classes of one common base object. I have implemented a virtual function in each derived class ("extractKey()") which returns a…
1
2