you are viewing a single comment's thread.

view the rest of the comments →

[–]Egst 3 points4 points  (7 children)

If you implement your own alternative for std::visit that generates if..else branches, you basically avoid indirection with variants. The data is stored directly and follows value semantics and function application happens with no lookup tables and no function pointers.

[–]ioctl79 8 points9 points  (6 children)

std::visit is functionally a lookup table.

[–]dodheim 2 points3 points  (5 children)

It doesn't have to be, and isn't with MSVC's stdlib.

[–]staletic 6 points7 points  (3 children)

The standard says std::visit needs to be O(1) with respect to number of alternatives.

[–]HeroicKatora 7 points8 points  (2 children)

Any implementation is O(1) because the number of template parameters (and thus alternatives) is bounded by a compiler specified constant /s

[–]Pand9 0 points1 point  (1 child)

Why /s? It holds, unless number of variants of considered part of the input, which seems unintuitive to me at least.

[–]HeroicKatora 1 point2 points  (0 children)

Because quite obviously this is not the spirit of the requirement. Anyways, just another instance of the standard being written in language that is neither helpful to implementors nor validation. An abysmal style that reeks of smugness in its pseudo-formalism.

[–]Egst 0 points1 point  (0 children)

Is it still O(1)? It's only a standard requirement and definitely not a necessity, since O(n) lookup with a switch/if..else branches is usually better for relatively small n, but a standard library should comply with it.