
On Thu, Nov 17, 2011 at 12:10 PM, Vicente J. Botet Escriba <vicente.botet@wanadoo.fr> wrote:
Le 17/11/11 13:15, Lorenzo Caminiti a écrit :
On Thu, Nov 17, 2011 at 4:33 AM, Vicente Botet<vicente.botet@wanadoo.fr> wrote:
lcaminiti wrote: I have not used yet C++ lambdas, couldn't the following be used to support const binding?
xtype x; xtype const& xcr; // const binder [xcr]() { assert( xcr == 0 ); }();
I can't check with a C++11 compiler right now, but my understand is "yes". Using decltype, you don't even have to repeat the type (to make maintenance easier). I think the most general way to write that is:
xtype x; decltype(x) const& const_ref_x = x; [xcr]() { assert( const_ref_x == 0 ); }();
The down sides are: 1) The lambda cannot refer to the bound variable using its original name x (this will actually look very strange in Boost.Contract if I had to indicate the user to use some magic names for variables in constant assertions). 2) The extra type manipulation.
Could you add the preceding lambda alternative to the Alternative sections once you could verify it?
Yes.
I would instead prefer the following if C++11 allowed it:
xtype x; [const&x]() { assert( x == 0 ); }();
But C++11 only allows to qualify the bind with the reference&x and not with the additional const const&x.
Maybe it is worth to post the idea to the comp.lang.c++ ML.
Other people have raised the same question (I can't find another reference I read a while back): http://stackoverflow.com/questions/3772867/lambda-capture-as-const-reference I guess I could ask comp.lang.c++ why C++11 does not allow const& bindings (I'm not really sure myself). Thanks. --Lorenzo