Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::proto user guide - compile error creating a lazy function
From: Eric Niebler (eric_at_[hidden])
Date: 2009-09-21 14:09:10


Tom Babbin wrote:
>> The problem is that proto::eval() takes its first argument by non-const
>> reference. The temporary expression returned by pow1 doesn't bind to it
>> as it's defined in the users' guide. Adding a const-qualification to the
>> return type solves the problem.
>>
>> I'll update the docs for 1.41. Thanks for the report.
>>
>
> Hi, Eric,
>
> Thanks for the quick response. Adding a const does fix the problem. However, I
> am relatively new to meta programming. How come a const expression binds to the
> first argument of eval, while a non-const does not?

Consider this:

  template<class T>
  void foo(T &) {}

  struct S {};
  S rvalue() { return S(); }
  S const const_rvalue() { return S(); }

Now the following invocation fails:

  foo( rvalue() ); // error, non-const rvalue cannot
                   // bind to an lvalue reference

But the following invocation succeeds:

  foo( const_rvalue() ); // OK

The reason is because in the second invocation, the template argument T
is deduced to be "B const", and an rvalue can bind to a const lvalue
reference. In the first invocation, T is deduced to "B" and an rvalue
cannot bind to a non-const lvalue reference.

HTH,

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com

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