Boost logo

Boost Users :

Subject: [Boost-users] [boost][proto] Type inference using transform
From: Manjunath Kudlur (keveman_at_[hidden])
Date: 2010-05-06 02:02:55


I am trying to decide whether the argument type to a function taking a
proto expression should be "const T&" or "T&" based on the expression
itself. Suppose the expression was "_1 = _2 + 1" I want the first
argument to a function, say FOO, to be T& and second argument to be
const T&. I write a transform argtype<int, typename> as follows :

#include <boost/proto/proto.hpp>
#include <boost/mpl/int.hpp>
#include <iostream>

struct arg_tag
{};

namespace proto = boost::proto;
namespace mpl = boost::mpl;

using namespace std;

template<int N>
struct arg
  : proto::or_<proto::nullary_expr<arg_tag, mpl::int_<N> > >
{};

template<typename T>
struct readonly
  : proto::callable {

  template<typename Sig>
  struct result;

  template<typename This>
  struct result<This()>
  {
    typedef const T& type;
  };
};

template<typename T>
struct readwrite
  : proto::callable {

  template<typename Sig>
  struct result;

  template<typename This>
  struct result<This()>
  {
    typedef T& type;
  };
};

template<int N, typename T>
struct argtype
  : proto::or_<
    proto::when<proto::assign<arg<N>, proto::_>, readwrite<T>()>
  , proto::otherwise<readonly<T>()>
>
{};
proto::nullary_expr<arg_tag, mpl::int_<0> >::type const _1 = {{}};
proto::nullary_expr<arg_tag, mpl::int_<1> >::type const _2 = {{}};

template<typename E>
void foo(const E &e)
{
  cout << typeid(typename boost::result_of<argtype<0,int>(const
E&)>::type).name() << "\n";
}

int main()
{

  foo(_1=_1+1);

  return 0;
}

I expected the typeid printed out by the foo function to be equivalent
to "int&" whereas I get "readwrite<int>". Any clues on where I am
going wrong?

Thanks,
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