r/cpp • u/_VZ_ wx | soci | swig • Jun 17 '15
Standardizing Variant: Difficult Decisions
https://www.justsoftwaresolutions.co.uk/cplusplus/standardizing-variant.html
31
Upvotes
r/cpp • u/_VZ_ wx | soci | swig • Jun 17 '15
View all comments
Show parent comment
3
u/eric_niebler Jun 18 '15
Imagine a variant
visitfunction that lets you pass a polymorphic function object. You pass avariant<int, string, float>and a function object with three function call operators that take anint, astringand afloatrespectively. The first and second returnintbut the third returnscomplex<float>. What should thevisitfunction return? Well, it could returnvariant<int, int, complex<float>>! Duplicateints, but they are semantically different. The first "slot" is occupied if the original variant held anintand the second is occupied if the original held astring.