boost::ptr_vector::sort with c++11 lambdas
I am trying to figure out if c++11 lambda can be used instead of the straight preducates. say, I have class A : public boost::noncopyable { ... } and boost::ptr_vector<A> lst; I'd like to use something like: lst.sort([&] (const A& v1, const A& v2) ->bool {return v1 < v2;}); Tutorial for ptr_vector says that predicates are wrapped in indirect_fun. May be this is the reason the sort fails to compile?
Den 11-11-2011 13:52, qplace skrev:
I am trying to figure out if c++11 lambda can be used instead of the straight preducates.
say, I have class A : public boost::noncopyable { ... }
and
boost::ptr_vector<A> lst;
I'd like to use something like:
lst.sort([&] (const A& v1, const A& v2) ->bool {return v1< v2;});
Tutorial for ptr_vector says that predicates are wrapped in indirect_fun. May be this is the reason the sort fails to compile?
Could be. What does the error message say? I'm thinking that the lambda function is not properly converted to a pointer to a function. An escape route may be to use std::sort() with a lambda taking two void*. -Thorsten
Thorsten Ottosen
Could be. What does the error message say?
I'm thinking that the lambda function is not properly converted to a pointer to a function.
An escape route may be to use std::sort() with a lambda taking two void*.
-Thorsten
The first error is:
1>c:\libraries\boost\boost_1_47_0\boost\utility\result_of.hpp(79): error
C2903: 'result' : symbol is neither a class template nor a function template
changing to void* results in the same error.
Test project is this one:
#include
Den 11-11-2011 15:30, qplace skrev:
Thorsten Ottosen
writes: Could be. What does the error message say?
I'm thinking that the lambda function is not properly converted to a pointer to a function.
An escape route may be to use std::sort() with a lambda taking two void*.
-Thorsten
The first error is: 1>c:\libraries\boost\boost_1_47_0\boost\utility\result_of.hpp(79): error C2903: 'result' : symbol is neither a class template nor a function template
changing to void* results in the same error.
I'm saying use std::sort() with void* arguments.
// lst.sort([&] (const A& v1, const A& v2) ->bool {return false;}); lst.sort([] (const A* v1, const A* v2) ->bool {return false;});
The first one is the correct one. But I think the problem is with
indirect_fun, or the code surrounding it. Does this work:
auto x = <lambda>;
boost::indirect_fun
Thorsten Ottosen
The first one is the correct one. But I think the problem is with indirect_fun, or the code surrounding it. Does this work:
auto x = <lambda>;
boost::indirect_fun
fun(x); ?
-Thorsten
int _tmain(int argc, _TCHAR* argv[])
{
boost::ptr_vector<A> lst;
// lst.sort([&] (const A& v1, const A& v2) ->bool {return false;});
auto x = [] (const A* v1, const A* v2) ->bool {return false;};
boost::indirect_fun
The following code compiles fine.
Is this what you want?
#define BOOST_RESULT_OF_USE_DECLTYPE
#include
Hello, Just one remark - you should inherit from boost::noncopyable privately. boost::noncopyable has no virtual dtor and no one should be able to obtain that base from any derived classes. At the same time noncopyable property will retain for the whole inheritance chain. Regards, Dmitriy. -- С уважением, Матисон Дмитрий.
Michel Morin
The following code compiles fine. Is this what you want?
#define BOOST_RESULT_OF_USE_DECLTYPE
Exactly, thank you for the solution, it compiles fine now. A follow-up question: what this #define does? Google'ing it it does not provide a reference to the doc, only to unrelated discussions. May be you have a direct link to the documentation page? Thanks.
qplace wrote:
Exactly, thank you for the solution, it compiles fine now. A follow-up question: what this #define does? Google'ing it it does not provide a reference to the doc, only to unrelated discussions. May be you have a direct link to the documentation page?
`indirect_fun` uses `result_of` to determine the return type of a functor. So the functor should have `result_type` nested-type or struct `result`. If the functor do not have them, we can use C++11 `decltype` feature to determine its return type (C++11 lambdas do not have them). To enable this feature, we have to define `BOOST_RESULT_OF_USE_DECLTYPE`. For BOOST_RESULT_OF_USE_DECLTYPE, please see http://www.boost.org/doc/libs/1_47_0/libs/utility/utility.htm#result_of Regards, Michel
Le 11/11/11 18:58, Michel Morin a écrit :
Exactly, thank you for the solution, it compiles fine now. A follow-up question: what this #define does? Google'ing it it does not provide a reference to the doc, only to unrelated discussions. May be you have a direct link to the documentation page? `indirect_fun` uses `result_of` to determine the return type of a functor. So the functor should have `result_type` nested-type or struct `result`. If the functor do not have them, we can use C++11 `decltype` feature to determine its return type (C++11 lambdas do not have them). To enable
qplace wrote: this feature, we have to define `BOOST_RESULT_OF_USE_DECLTYPE`.
For BOOST_RESULT_OF_USE_DECLTYPE, please see http://www.boost.org/doc/libs/1_47_0/libs/utility/utility.htm#result_of
Hi, is there any reason result_of couldn't use decltype when available? That is, could Boost define BOOST_RESULT_OF_USE_DECLTYPE when decltype is supporeted? Best, Vicente
Vicente J. Botet Escriba wrote:
is there any reason result_of couldn't use decltype when available? That is, could Boost define BOOST_RESULT_OF_USE_DECLTYPE when decltype is supporeted?
First, (almost) all current implementation of decltype does not support N3276.
Non-N3276-decltype requires type-completeness, while traditional result_of
does not. So result_of with non-N3276-decltype is incompatible with
the traditional result_of.
We should proceed gradually even if N3276-decltype is available, because
* decltype-based result_of can break user codes with (unintended) use
of result_of.
* Implementation of N3276-decltype might be buggy for some time.
Daniel already made a first step by updating the documentation.
The doc on trunk says:
In a future release, BOOST_RESULT_OF_USE_DECLTYPE may be enabled
by default on compilers that support decltype, so if you use the above
protocol please take care to ensure that the result_type and
result<> members accurately represent the result type.
If you wish to continue to use the protocol on compilers that
support decltype, use boost::tr1_result_of, which is also defined
in
Le 12/11/11 05:34, Michel Morin a écrit :
is there any reason result_of couldn't use decltype when available? That is, could Boost define BOOST_RESULT_OF_USE_DECLTYPE when decltype is supporeted? First, (almost) all current implementation of decltype does not support N3276. Non-N3276-decltype requires type-completeness, while traditional result_of does not. So result_of with non-N3276-decltype is incompatible with
Vicente J. Botet Escriba wrote: the traditional result_of.
We should proceed gradually even if N3276-decltype is available, because * decltype-based result_of can break user codes with (unintended) use of result_of. * Implementation of N3276-decltype might be buggy for some time.
Daniel already made a first step by updating the documentation. The doc on trunk says: In a future release, BOOST_RESULT_OF_USE_DECLTYPE may be enabled by default on compilers that support decltype, so if you use the above protocol please take care to ensure that the result_type and result<> members accurately represent the result type. If you wish to continue to use the protocol on compilers that support decltype, use boost::tr1_result_of, which is also defined in
.
Thanks for the clarification. So if IUC, the use of BOOST_RESULT_OF_USE_DECLTYPE in the user code could break other parts of her application, isn't it? Best, Vicente
Vicente J. Botet Escriba wrote:
We should proceed gradually even if N3276-decltype is available, because * decltype-based result_of can break user codes with (unintended) use of result_of. * Implementation of N3276-decltype might be buggy for some time.
Thanks for the clarification. So if IUC, the use of BOOST_RESULT_OF_USE_DECLTYPE in the user code could break other parts of her application, isn't it?
Right. There are two types of breakage. (But note that the correct support of the traditional result_of protocol and the correct usage of result_of should not make any breakage with BOOST_RESULT_OF_USE_DECLTYPE + N3276-decltype. So, I think, you don't have to worry about the breakage too much.) 1) Breakage caused by type-completeness requirement of non-N3276-decltype Boost.Proto uses boost::tr1_result_of to avoid this problem. boost::tr1_result_of uses the traditional implementation of result_of even if BOOST_RESULT_OF_USE_DECLTYPE is defined. 2) Breakage caused by incorrect support or incorrect usage of result_of Some codes using Boost.Fusion or Boost.Phoenix fail to compile if BOOST_RESULT_OF_USE_DECLTYPE is defined. http://thread.gmane.org/gmane.comp.parsers.spirit.general/24027 https://svn.boost.org/trac/boost/ticket/5687 Regards, Michel
participants (5)
-
Dmitriy Matison
-
Michel Morin
-
qplace
-
Thorsten Ottosen
-
Vicente J. Botet Escriba