[variant] Variants of references, when initialized from temporary objects, should own the temporary object.

The following code: #include <iostream> #include "boost/variant.hpp" struct Z { Z(int x) : x(x) { std::cout << "Created a Z with x == " << x << std::endl; } ~Z() { std::cout << "Destroyed a Z with x == " << x << std::endl; } int x; }; void f(boost::variant<const Z&> const& x) { std::cout << boost::get<const Z&>(&x)->x << std::endl; } int main() { int x = 17; std::cout << "A" << std::endl; f(x); std::cout << "B" << std::endl; } produces this output on gcc with boost 1.34.1: A Created a Z with x == 17 Destroyed a Z with x == 17 -11257 B Obviously, x holds a Z reference to a temporary Z, which is destroyed before f() returns. Needless to say, this is in stark contrast with what happens if f had been defined to take a const Z& instead of a boost::variant<const Z&> or a boost::variant<const Z&> const&. Is this a bug? --Joachim
participants (1)
-
Joachim Kupke