C++0x TR1: result_type and binders to class datamembers
During migrating some portions of my code under TR1 library shipped
with gcc 4.3.2, I was suprised with the following strange thing.
This simple code generates some error during compilation:
#include <iostream>
#include <map>
#include <algorithm>
#include <iterator>
#include <tr1/functional>
#include <boost/bind.hpp>
#include <boost/iterator/transform_iterator.hpp>
int main(int, char*[])
{
std::map<int, int> test_map;
test_map[0] = 10;
test_map[1] = 5;
test_map[5] = 8;
test_map[3] = 20;
typedef std::map<int,int>::value_type vt;
std::copy(boost::make_transform_iterator(test_map.begin(),
std::tr1::bind(&vt::second, std::tr1::placeholders::_1)),
boost::make_transform_iterator(test_map.end(), std::tr1::bind
(&vt::second, std::tr1::placeholders::_1)),
// std::copy(boost::make_transform_iterator(test_map.begin(),
boost::bind(&vt::second, _1)),
// boost::make_transform_iterator(test_map.end(), boost::bind
(&vt::second, _1)),
// std::ostream_iterator<int>(std::cout, "\n"));
}
Error is:
error: no type named 'result_type' in 'struct
std::tr1::_Bind<std::tr1::_Mem_fn<int std::pair<const int, int>::*> ()
(std::tr1::_Placeholder<1>)>'
test2.cpp: In function 'int main(int, char**)':
Compilator is gcc 4.3.2 (MinGW build). Same errors generates Visual C+
+ 9.0 with SP1.
When I replace TR1 binders with boost binders compilation was
successfuly done.
During investigation of this problem I look into new standard working
draft and found there following phrase ([func.require] section, clause
3):
----------------------------------------------------------------------
If a call wrapper (20.7.1) has a weak result type the type of its
member type result_type is based on the
type T of the wrapper?s target object (20.7.1):
? if T is a function, reference to function, or pointer to function
type, result_type shall be a synonym
for the return type of T;
? if T is a pointer to member function, result_type shall be a synonym
for the return type of T;
? if T is a class type with a member type result_type, then
result_type shall be a synonym for
T::result_type;
? otherwise result_type shall not be defined.
----------------------------------------------------------------------
Does it really mean what by the new standard "call wrapers" for class
datamembers arn't support 'unary function' concept? And when I want to
make a binder for class datamember (like in the example) have I to use
boost binders instead of TR1 binders? Or I don't understand something?
I understand what class datamember is not 'callable object' as-is. But
existing implementations of binders offer to developers quite useful
feature which helps them don't mind about real kind of object they
want to bind. And one more question. What must I do with example shown
above to make it TR1-conformant *without* boost binders. I. e. what
must I pass to the transform iterator to make this example compilable?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]