Boost logo

Boost :

From: Joachim Kupke (joachim_at_[hidden])
Date: 2007-11-05 21:57:31


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


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk