variant no compile time check on get<> type parameter
Hello In order to minimize heap allocations but still allow for some OOP designs I needed a sort of union but one that it works for more than POD types. So I implemented such a thing myself to find out recently that boost::variant might do this already (boost does that often ;) ). However, initial testing for boost::variant shows that that there is no compile time check on the types allowed to instantiate boost::get on a variant, example: struct A {}; struct B {}; struct C{}; int main() { boost::variant v v; boost::get<C>(v); } Compiles fine (gcc 4.1.1, boost 1.33.1) but errors at runtime throwing a bad_get exception from get. It seems to me that the get<> on variant somehow works for any type not checking if the type parameter for get matches one in the type sequence of the given variant. Maybe I miss something but shouldn't get on variant have a compile time type check ? (this would catch at compile time common errors for variant use cases I think) -- Mihai RUSU Email: dizzy@roedu.net GPG : http://dizzy.roedu.net/dizzy-gpg.txt WWW: http://dizzy.roedu.net "Linux is obsolete" -- AST
Dizzy wrote:
However, initial testing for boost::variant shows that that there is no compile time check on the types allowed to instantiate boost::get on a variant, example:
struct A {}; struct B {}; struct C{};
int main() { boost::variant v v; boost::get<C>(v); }
Compiles fine (gcc 4.1.1, boost 1.33.1) but errors at runtime throwing a bad_get exception from get. It seems to me that the get<> on variant somehow works for any type not checking if the type parameter for get matches one in the type sequence of the given variant.
Maybe I miss something but shouldn't get on variant have a compile time type check ? (this would catch at compile time common errors for variant use cases I think)
I agree.
It might be true that the version without compile-time has its just
usages, but the version with the compile-time check is much more useful.
So I found myself adding the following functions:
template
participants (2)
-
Dizzy
-
Yuval Ronen