|
Boost Users : |
Subject: [Boost-users] [boost-users][proto] Types of arguments to operator()
From: Manjunath Kudlur (keveman_at_[hidden])
Date: 2010-05-07 13:55:41
I realize that this may be more of a C++ language question rather than
proto specific. But I will ask it anyway. I wrote a transform that
infers some type information for the placeholders from an expression.
Say I have the expression _1 = _2 + 1, the transform can tell me that
the type of the first argument to operator() of the expression wrapper
should be "T &" and the type of the second should be "const T&". Here
is the transform (thanks to JoelF for his help on this) :
struct arg_tag {};
template<int N>
struct arg
: proto::or_<proto::nullary_expr<arg_tag, mpl::int_<N> > >
{};
template<typename T>
struct readonly
: proto::callable {
typedef const T& result_type;
};
template<typename T>
struct readwrite
: proto::callable {
typedef T& result_type;
};
template<int N, typename T>
struct argtype
: proto::or_<
proto::when<proto::assign<arg<N>, proto::_>, readwrite<T>()>
, proto::otherwise<readonly<T>()>
>
{};
namespace boost { namespace proto {
template<class T> struct is_callable< readonly<T> > : mpl::true_ {};
template<class T> struct is_callable< readwrite<T> > : mpl::true_ {};
} }
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? I will be grateful for
any advice. I am attaching the complete source code (fails to
compile).
-- Manjunath http://nonchalantlytyped.net/blog/musings/
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