Is boost :: a variant of rocket science? (And therefore should I avoid this for simple problems?)

OK, so I have this little small corner of my code where I would like my function to return any of ( int , double , CString ) to clean up the code a bit.

So, I think: there is no problem writing a small unified shell struct with three members, etc. But wait! Didn't I read boost::variant ? Wasn't that exactly what I needed? That would save me from clutter with a wrapper structure! (Note that I already have a boost library available in my project.)

So, I launch my browser, go to Chapter 28. Boost.Variant and lo and behold:

The variant-variant template is a safe, general, stack-based discriminatory union container, offering a simple solution for manipulating an object from a heterogeneous set of types [...]

Excellent! That's what I need!

But then it continues:

Boost.Variant vs. Boost.any

  • Boost.Any makes little use of template metaprogramming methods (avoiding potentially hard-to-read error messages and significant processor and memory requirements at compile time).

[...]

Troubleshooting

"Internal heap limit reached" - Microsoft Visual C ++. The compiler option / ZmNNN may increase the memory allocation limit. NNN represents the percentage of scaling (i.e. 100 indicates the default limit). (Try / Zm200.)

[...]

Oh oh Thus, using boost :: variant can significantly increase compilation time and generate hard-to-read error messages. What if someone moves my use of boost :: variant to a common header, will our project suddenly take longer to compile? Am I representing an (optional) complex type?

Should I use boost::variant for my simple tiny problem ?

+4
source share
4 answers

Boost.variant is not so complicated, IMHO. Yes, it is based on templates, but does not use any really complex C ++ function. I used quite a bit and have nothing. I think in your case it will help to better describe what your code does.

Another way of thinking is to transform what this function returns into a more semantically rich structure / class, which allows you to interpret which internal element is interesting, but it depends on your design.

+5
source

Typically, use boost::variant if you want a discriminated union ( any for unknown types) to treat it as something equivalent to how void* used in C).

Some of the benefits include exception handling, the potential use of less space than the sum of the sample sizes, and the type of discriminated “visit”. Basically, the material you want to perform on a discriminatory association.

However, for boost::variant to be effective, at least one of the types used must be “easy” to build (read the documentation for more details on what “easy” means).

+7
source

This type of boost element comes from functional programming, where you have options in every corner.

It should be a way to have a type-safe approach to returning a kind of value that can have many exact types. This means it’s helpful to solve your problem, but you should consider whether this is really what you need to do.

The added value compared to other approaches that try to solve the same problem should be type safety (you won’t be able to place everything you want inside the variant without noticing, unlike void* )

+1
source

I do not use it because for me it is a symptom of poor design.

Either your method should return an object that implements a certain interface, or it should be divided into more than one method. In any case, the design should be reviewed.

0
source

All Articles