6

関数ポインターによってテンプレート化されたプロト変換を作成して、コードを再利用したいと思います。

template <typename Ret, typename A0, typename A1, Ret func(A0,A1)>
struct apply_func : proto::callable
{
  // Do something with func
};

ただし、関数自体は多態的であるため、正確なシグネチャを指定したくありません。

コードを次のように簡略化したバージョンを次に示します (現在の質問とは無関係であると思われる技術的な理由から、外部変換を使用しています - それらなしでは再帰を機能させることができませんでした):

template<typename R, typename A0, typename A1>
R plus_func(A0 lhs, A1 rhs) { return lhs+rhs; }

template<typename R, typename A0, typename A1>
R minus_func(A0 lhs, A1 rhs) { return lhs-rhs; }

struct my_grammar;
struct plus_rule : proto::plus<my_grammar, my_grammar> {};
struct minus_rule : proto::minus<my_grammar, my_grammar> {};

struct my_grammar
: proto::or_<
  proto::when<proto::terminal<proto::_>, proto::_value> 
, proto::when<plus_rule, proto::external_transform > 
, proto::when<minus_rule, proto::external_transform > 
>
{};

struct my_external_transforms 
  : proto::external_transforms<
      proto::when<plus_rule, apply_func<plus_func>(my_grammar(proto::_left),my_grammar(proto::_right), proto::_state)>
    , proto::when<minus_rule, apply_func<minus_func>(my_grammar(proto::_left),my_grammar(proto::_right), proto::_state)>
    >
{};

appy_func テンプレートへの引数が欠落しているため、これはコンパイルされません。解決策はありますか?

4

1 に答える 1

3

You have multiple problem in your code:

  • you cannot take a template function pointer without specifying its template parameter as the template won't exist until the function get instanciated.
  • second point, Ret(A,B) is a function type not a function pointer type.
  • function pointer are a bit raw as abstraction goes, same can be achieved by a functor which also solve your problem as a polymorphic fnction object is a single, non template type.
  • final technical point, template transform can't use proto::callable, you have to specialize boost::proto::is_callable explicitly. This is due to a langauge limitation on how the inheritance is detected.

Perusing your pseudo code, I'll go for something like :

struct plus_func
{
  template<class Sig> struct result;

  template<class This,class A, class B> 
  struct result<This(A,B)>
  {
     typedef /*whatever*/ type;
  };

  template<class A, class B>
  typename result<plus_func(A const&,B const&)>::type
  plus_func(A const& lhs, B const& rhs) 
  { 
    return lhs+rhs; 
  }
};

struct minus_func
{
  template<class Sig> struct result;

  template<class This,class A, class B> 
  struct result<This(A,B)>
  {
     typedef /*whatever*/ type;
  };

  template<class A, class B>
  typename result<minus_func(A const&,B const&)>::type
  plus_func(A const& lhs, B const& rhs) 
  { 
    return lhs-rhs; 
  }
};

struct my_grammar;
struct plus_rule : proto::plus<my_grammar, my_grammar> {};
struct minus_rule : proto::minus<my_grammar, my_grammar> {};

struct my_grammar
: proto::or_<
  proto::when<proto::terminal<proto::_>, proto::_value> 
, proto::when<plus_rule, proto::external_transform > 
, proto::when<minus_rule, proto::external_transform > 
>
{};

struct my_external_transforms 
  : proto::external_transforms<
      proto::when<plus_rule, apply_func<plus_func>(my_grammar(proto::_left),my_grammar(proto::_right), proto::_state)>
    , proto::when<minus_rule, apply_func<minus_func>(my_grammar(proto::_left),my_grammar(proto::_right), proto::_state)>
    >
{};

Type returned by each PFO has to be computed or specified. Beware that A,B can be const/ref qualified and may need stripping before doing type computation.

Sidenote : external_transform are not required at all for recursive rules. I guess point 4 (template callable) was what made it not work.

于 2012-03-09T15:49:31.377 に答える