2013/8/4 gast128
Dear all,
I get a compilation error with Boost.Variant (which compiles fine on previous releases) with vstudio 2010 when a function returns a const Boost.Variant, e.g.:
typedef boost::variant
Variant; const Variant GetVar() { return Variant(); }
void TestVariantDnc() { const Variant var = GetVar(); }
This gives error C2666 ('boost::variant
::convert_construct' : 3 overloads have similar conversions'). Shall a file a ticket or do I something wrong?
Let me explain why this is not a good C++11 code any more.
Take a look at the following code:
const some_type GetVar()
{
return some_type();
}
Compiler instead of that will generate the following:
const some_type&& GetVar()
{
return some_type();
}
So now, when you'll try to do something like this
const some_type var = GetVar();
Compiler will convert that to the following:
const some_type var(GetVar());
Because usually there are no constructors for const rvalues, compiler will
use the copy constructor for some_type (`some_type(const some_type&)`).
So, adding a const disables all the move constructors and rvalue
optimizations.
However that does not mean that Boost.Variant can fail with that error. To
fix that error you can use the following trick:
* Find the line that has enable_if