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

  • Home
  • SEARCH
  • 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 6612535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:06:36+00:00 2026-05-25T20:06:36+00:00

I don’t know if this can even be achivieable, but given these set of

  • 0

I don’t know if this can even be achivieable, but given these set of functions\class:

float plus1(float x) { return x+1; }
float div2(float x) { return x/2.0f; }
template <typename T>
class chain {
public:
    chain(const T& val = T()) : val_(val) {}
    chain& operator<<( std::function<float (float)> func ) {
    val_ = func(val_);
    return *this;
  }
  operator T() const {
    return val_;
  }
  T val_;
};

I can chain functions operating on floats like this:

float x = chain<float>(3.0f) << div2 << plus1 << div2 << plus1;

However, I’d like to generalize\extend this to being able to convert between types and have functions with arguments. Unfortunately I’m not smart enough to figure out how, or if, this can be done.
Too be more specific I’d like to be able to do something like this (Where operator<< is just an arbitary choice, and preferably I dont even have to write “chain” at the beginning);
Also, these are just dummy examples, I do not intend to use it for arithmetics.

std::string str = chain<float>(3.0) << mul(2.0f) << sqrt << to_string << to_upper;

or

vec3d v = chain<vec3i>(vec3i(1,1,1)) << normalize << to_vec3<double>;

Any ideas?

  • 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-25T20:06:37+00:00Added an answer on May 25, 2026 at 8:06 pm

    A general and extendable solution using boost::proto :

    #include <iostream>
    #include <boost/proto/proto.hpp>
    
    namespace bp = boost::proto;
    
    // -----------------------------------------------------------------------------
    // perform is a callable transform that take a function_ terminal and execute it
    // -----------------------------------------------------------------------------
    struct perform : bp::callable
    {
      template<class Sig> struct result;
      template<class This, class Func, class In>
      struct result<This(Func,In)> 
           : boost::result_of<typename boost::remove_reference<Func>::type(In)> {};
    
      template<class Func, class In>
      typename result<perform(Func &,In)>::type
      operator()( Func& f, In& in ) const
      {
        return f(in);
      }
    };
    
    // -----------------------------------------------------------------------------
    // Grammar for chaining pipe of functions
    // -----------------------------------------------------------------------------
    struct pipeline_grammar
    : bp::or_<
        bp::when<
            bp::bitwise_or<pipeline_grammar,pipeline_grammar>
              , pipeline_grammar(
                    bp::_right
                  , pipeline_grammar(bp::_left,bp::_state)
                    )
            >
          , bp::when<
                bp::terminal<bp::_>
              , perform(bp::_value, bp::_state) 
        >
    > {};
    
    // -----------------------------------------------------------------------------
    // Forward declaration of the pipeline domain
    // -----------------------------------------------------------------------------
    struct pipeline_domain;
    
    // -----------------------------------------------------------------------------
    // A pipeline is the top level DS entity
    // -----------------------------------------------------------------------------
    template<class Expr>
    struct  pipeline : bp::extends<Expr,pipeline<Expr>, pipeline_domain>
    {
      typedef bp::extends<Expr, pipeline<Expr>, pipeline_domain> base_type;
      pipeline(Expr const &expr = Expr()) : base_type(expr) {}
    
      // ---------------------------------------------------------------------------
      // A pipeline is an unary callable object
      // ---------------------------------------------------------------------------
      template<class Input>
      typename boost::result_of<pipeline_grammar(pipeline,Input)>::type
      operator()(Input const& in) const
      {
        pipeline_grammar evaluator;
        return evaluator(*this,in);
      }
    };
    
    // -----------------------------------------------------------------------------
    // the pipeline_domain make pipeline expression macthes pipeline_grammar
    // -----------------------------------------------------------------------------
    struct pipeline_domain 
         : bp::domain<bp::generator<pipeline>,pipeline_grammar>
    {};
    
    // -----------------------------------------------------------------------------
    // Takes a PFO instance and make it a pipeline terminal
    // -----------------------------------------------------------------------------
    template<class Func>
    typename bp::result_of::
    make_expr<bp::tag::terminal, pipeline_domain,Func>::type
    task( Func const& f )
    {
      return bp::make_expr<bp::tag::terminal,pipeline_domain>( f );
    }
    
    //--------------------------- Examples --------------------
    
    struct return_value
    {  
      template<class Sig> struct result;
      template<class This, class T>
      struct result<This(T)> : bp::detail::uncvref<T>
      {};
    
      return_value(int i = 1) : factor(i) {}
    
      template<class T> 
      T operator()(T const& in) const
      {
        return in*factor;
      }
    
      int factor;
    };
    
    struct say_hi
    {
      typedef void result_type;
    
      template<class T> 
      void operator()(T const& in) const
      {
        std::cout << "Hi from value = " << in << "\n";
      }
    };
    
    int main()
    {
      return_value r1,r2(5);
      (task(r1) | task(r2) | task(say_hi())) (7); // SHould print 35
    
      float k = 10,r;
      r = (task(r2) | task(r2) | task(r2) | task(r2))(k);
      std::cout << r << "\n"; // Should print 6250
    }
    

    The basic idea is to wrap function objects as proto terminals, build a small | based grammar and let the proto system deals with the composition.

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

Sidebar

Related Questions

don't know better title for this, but here's my code. I have class user
Don't know if anyone can help me with this or if it's even possible.
Don't know why but I can't find a solution to this. I have 3
(Don't know if this is strictly on-topic, but I don't see any better Stack
I don't know if this question is trivial or not. But after a couple
Don't know if this is the right place to ask this, but I will
Don't know if this is an eclipse specific problem but whenever I declare a
I don't know if this has been asked before, but what i'd like to
Don't ask me why but i can't use this method because I need to
Don't know why this doesn't work. I can't do implicit animation for a newly

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.