Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost-users][proto] Types of arguments to operator()
From: Eric Niebler (eric_at_[hidden])
Date: 2010-05-07 14:10:38


On 5/7/2010 10:55 AM, Manjunath Kudlur wrote:
> Now, given an expression type E, I can say "typename
> boost::result_of<argtype<0, T0>(const E &)>::type" and it will be
> either "T0 &" or "const T0 &" depending on how _1 appears in the
> expression E. Using this, I tried to write an expression wrapper with
> operator() overloaded as follows :
>
> template<typename T0>
> void operator()(typename boost::result_of<argtype<0, T0>(const Expr
> &)>::type arg0) const
> {
> cout << "arg0 " << arg0 << "\n";
> }
>
> But the compiler fails to find a match with this overloaded version
> when I write, say, (_1)(10). Is it some fundamental C++ restriction
> that causes this, or am I missing something?

There's a fundamental restriction. T0 is in a nondeduced context here.
Imagine a template Int like:

  template<typename T> struct Int { typedef int type; };

and used like

  template<typename T>
  void foo(typename Int<T>::type) ...

  foo(1);

What can the compiler say about T? Any choice will work, so the compiler
has no way to pick one.

What you need to do instead is define both const and non-const overloads
and either use SFINAE to get one to drop out, or else (better, IMO)
assert within the function that the correct one has been chosen:

  template<typename T0>
  void operator()(T0 & arg0) const
  {
    BOOST_MPL_ASSERT((
      is_same<
        T0 &,
        typename boost::result_of<argtype<0, T0>(const Expr &)>::type
>
    ));
    cout << "arg0 " << arg0 << "\n";
  }

  template<typename T0>
  void operator()(T0 const & arg0) const
  {
    BOOST_MPL_ASSERT((
      is_same<
        T0 const &,
        typename boost::result_of<argtype<0, T0>(const Expr &)>::type
>
    ));
    cout << "arg0 " << arg0 << "\n";
  }

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