我可以知道转换中与 boost::proto::_ 匹配的类型吗?

发布于 2025-01-04 20:05:17 字数 740 浏览 2 评论 0原文

在 boost::proto 手册中,有一个与 std::transform<...> 类型的终端匹配的语法示例:

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

我想编写一个对 proto::_ 类型执行某些操作的转换。 例如,当匹配 proto::terminal< 时std::复杂< 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) >  
{};  

In the boost::proto manual, there is an example of a grammar that matches terminals of type std::transform<...>:

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

I would like to write a transform that does something with the type of the proto::_.
For example, when matching a proto::terminal< std::complex< T > >, it returns a boost::shared_ptr < T > .

Is this possible?

Another way to state my question is, how do I make the following snippet work?

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) >  
{};  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一世旳自豪 2025-01-11 20:05:17

您的显示转换将更容易作为多态函数对象处理:

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) >  
{};  

Your Show transform will be easier to handle as a 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) >  
{};  

Your answer on the other problem is :

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) >  
{};  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文