3

boost :: protoマニュアルには、タイプstd :: transform <...>:の端末に一致する文法の例があります。

struct StdComplex
  : proto::terminal< std::complex< proto::_ > >  
{};

proto::_のタイプで何かを行う変換を書きたいと思います。たとえば、proto :: terminal <std :: complex <T>>と一致すると、boost ::shared_ptr<T>が返されます。

これは可能ですか?

私の質問を述べる別の方法は、次のスニペットを機能させるにはどうすればよいですか?

template<typename T>
struct Show : proto::callable  
{
    typedef T result_type;

    result_type operator()(T& v)
    {
        std::cout << "value = " << v << std::endl;
        return v;
    }
};


struct my_grammar
: proto::when<proto::terminal<proto::_ >, Show<??? what comes here ???>(proto::_value) >  
{};  
4

1 に答える 1

3

Show 変換は、Polymorphic Function Object として扱いやすくなります。

struct Show : proto::callable  
{
  template<class Sig> struct result;

  template<class This, class T>
  struct result<This(T)>
  {
    typedef T type;
  };

  template<class T> T operator()(T const& v) const
  {
      std::cout << "value = " << v << std::endl;
      return v;
  }
};   

struct my_grammar
: proto::when<proto::terminal<proto::_ >, Show(proto::_value) >  
{};  

他の問題に対するあなたの答えは次のとおりです。

struct to_shared : proto::callable  
{
  template<class Sig> struct result;

  template<class This, class T>
  struct result<This(T)>
  {
    typedef typename T::value_type base;
    typedef shared_ptr<base> type;
  };

  template<class T> 
  typename result<to_share(T)>::type operator()(T const& v) const
  {
    // stuff
  }
};


struct my_grammar
: proto::when<proto::terminal<complex<proto::_> >, to_shared(proto::_value) >  
{};  
于 2012-02-16T20:05:07.710 に答える