c++ - boost::variant of const and non-const -
i want create template< typename f > using t = boost::variant< f, f const >;
type store read-only , read-write accessible values same std::vector< t >
. i've got following programming problem:
#include <iostream> #include <cstdlib> #include <boost/variant.hpp> int main() { using f = double; using cv = boost::variant< f const, f >; f const c = 0.0; cv c(c); f v = 0.0; cv v(v); std::cout << c.which() << ' ' << v.which() << std::endl; return exit_success; }
output: 1 1
. how can store const
version of value of type f
?
this not solution, usefull:
#include <iostream> #include <functional> #include <cstdlib> #include <boost/variant.hpp> int main() { using f = double; using cv = boost::variant< std::reference_wrapper< f const >, std::reference_wrapper< f > >; f const c = 0.0; cv c(std::ref(c)); f v = 0.0; cv v(std::ref(v)); std::cout << c.which() << ' ' << v.which() << std::endl; return exit_success; }
this requires additional space store data if any. in cases, may more appropriate 1 in original formulation of question.
Comments
Post a Comment