Boost logo

Boost Users :

Subject: [Boost-users] [Lambda] Misplaced "const" causes runtime error?
From: Paul Baker (paulbaker8_at_[hidden])
Date: 2009-02-16 04:29:41


Hi,

The following program, as expected, outputs "1 1 1":

#include <algorithm>
#include <iostream>
#include <vector>
#include <boost/mem_fn.hpp>
#include <boost/bind.hpp>
#include <boost/lambda/bind.hpp>

int main()
{
    std::vector<std::pair<int, int> > pairs;
    pairs.push_back(std::make_pair(1, 1));
    std::vector<int> result;
    std::transform(pairs.begin(), pairs.end(), std::back_inserter(result),
boost::mem_fn(&std::pair<int, int>::first));
    std::transform(pairs.begin(), pairs.end(), std::back_inserter(result),
boost::bind(&std::pair<int, int>::first, _1));
    std::transform(pairs.begin(), pairs.end(), std::back_inserter(result),
boost::lambda::bind(&std::pair<int, int>::first, boost::lambda::_1));
    std::cout << result[0] << ' ' << result[1] << ' ' << result[2];
}

Changing "std::pair<int, int>" to "std::pair<const int, int>" on the line
using boost::mem_fn or the line using boost::bind causes a compile-time
error.

However, performing the same change on the line using boost::lambda::bind
leads to a successful compile - yet at run-time result[2] is set to a
seemingly random value. This has been tested on Visual C++ 7.1 SP1, 8.0 SP1
and 9.0 SP1.

Is this expected behaviour? I was expecting to see either a compile-time
error or run-time success.

Thanks



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net