Subject: [Boost-bugs] [Boost C++ Libraries] #4967: apply_visitor_delayed_t 's operator() is not const correct
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2010-12-08 16:23:51
#4967: apply_visitor_delayed_t 's operator() is not const correct
----------------------------------+-----------------------------------------
Reporter: zhuo.qiang@⦠| Owner: ebf
Type: Patches | Status: new
Milestone: To Be Determined | Component: variant
Version: Boost 1.45.0 | Severity: Problem
Keywords: |
----------------------------------+-----------------------------------------
The operator() overload in Boost.Variant's boost::apply_visitor_delayed_t
is not const qualified which I think could be safely add.
The benefit of adding const is as following:
There are some generic function need to store and call the
apply_visitor_delayed_t instance, transform_iterator & transform_range,
for example. The following code will not compile without the const
quality:
----------------------------------------------------
using namespace boost;
using namespace std;
typedef variant<int, string> V;
struct times_2_t : public static_visitor<int>
{
template<class T>
int operator()(T const& t) const
{
return 2 * t;
};
int operator()(string const& s) const
{
return 2 * s.size();
};
};
BOOST_AUTO_TEST_CASE(Test)
{
vector<V> all;
all.push_back(1);
all.push_back(2);
times_2_t times_2;
int const result = boost::accumulate(
all | boost::adaptors::transformed(apply_visitor(times_2)),
0);
BOOST_CHECK_EQUAL(6, result);
}
---------------------------------------
One way to fix this is using a boost::funciton to hold the
apply_visitor_delayed_t instance and pass the wrapper to transformed:
---------------------------------------------------------
times_2_t times_2;
function<int(V const&)> f = apply_visitor(times_2);
int const result = boost::accumulate(
all | boost::adaptors::transformed(apply_visitor(f)),
0);
-------------------------------------------------------
However, this will make simple things complex and have runtime cost.
So a more direct fix would be add the harmless const qualify to the
operator() for apply_visitor_delayed_t
The attached is a simply patch:
-- Ticket URL: <https://svn.boost.org/trac/boost/ticket/4967> Boost C++ Libraries <http://www.boost.org/> Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:05 UTC