Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7611223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:37:04+00:00 2026-05-31T01:37:04+00:00

I would like to reuse code by writing a proto transform which is templated

  • 0

I would like to reuse code by writing a proto transform which is templated by a function pointer:

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

However, the function itself is polymorphic so I do not want to specify its exact signature.

A simplified version of what I would like my code to look like follows (I am using external transforms for a technical reason that I think is unrelated to my current question – I could not get the recursion working without them):

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

That does not compile because there are missing arguments to the appy_func template. Is there a solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:37:06+00:00Added an answer on May 31, 2026 at 1:37 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a python script which I would like to be able to both
I would like to reuse some existing code in our code base that accepts
I would like to reuse the exact same font-face etc... like Android uses in
Would like to know the c# code to actually retrieve the IP type: Static
I like to keep my code as DRY as possible and would like to
In order for reuse ability on iOS, I would like to write the logic
I have a number of xaml sub-workflows that I would like to reuse in
I would like to reuse a figure I create in Matlab script. fig1 =
I'm developing a software layer that I would like to reuse several time for
I would like to create a simple drop down list with static values which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.