tags:

views:

153

answers:

2

How efficient is dispatching on a boost::variant ?

If it's a switch statement, it should only take O(1) time, but as far as I know, template metaprogrammign can only generate if's, which would put boost::variant dispatchs at a runtime overhead of O(n), where n = number of types in the variant.

Can anyone confirm/deny/enlighten me on this?

Thanks!

+1  A: 

but as far as I know, template metaprogrammign can only generate if's, which would put boost::variant dispatchs at a runtime overhead of O(n),

No: template metaprogramming ifs are evaluated at compile time so if the overhead is defined by template metaprogramming, it will be a compile time overhead. Runtime overhead will then be constant.

Caveat: I do not know how boost::variant dispatch works. But if it’s implemented using compile-time if, it will behave as described above.

Konrad Rudolph
+1. Also, a code generator can very well turn nested or chained `if` s testing the same var against a succession of values into a `switch`. This is the kind of thing you don't worry about until you verify it's happening wrong.
Potatoswatter
+2  A: 

Looking at the source, it should be constant time. Boost uses Boost.PreProcessor to generate a switch-table, and keeps track of which index it should jump to (via the type being stored).

GMan
Which is EXACTLY what I suggested the last TWO times he asked this question TODAY.
Potatoswatter
@Potatoswatter: rofl <3 umad bro? :P
GMan
It wouldn't be so bad if I'd gotten an upvote.
Potatoswatter
@Potatoswatter: Fix'd. :3
GMan
thx,,,,, lol :)
Potatoswatter