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 4619538
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:26:16+00:00 2026-05-22T02:26:16+00:00

We have a helper function in our codebase to concatenate two (Windows) path strings:

  • 0

We have a helper function in our codebase to concatenate two (Windows) path strings:

CString AppendPath(CString const& part1, CString const& part2);

It is often used in this way:

const CString filePath = AppendPath(AppendPath(AppendPath(base, toplevel), sub1), filename);

This is rather acceptable, but it got me wondering if there is some possibility in C++ (or C++0x) to use a (template?) function to chain binary function calls together.

That is, given a function T f(T arg1, T arg2) is it possible to write a function T ncall(FnT fn, T arg1, T arg2, T arg3, ...) that will call f like in my example above and return the result?

// could roughly look like this with my example:
const CString filePath = ncall(&AppendPath, base, toplevel, sub1, filename);

Please, this question is about the transformation and not about the best way to handle or concatenate path strings!


Edit: Thanks to deft_code‘s answer for providing the correct term for what I was asking for: Fold (higher-order function). (Note that I have settled on accepting the answer of Matthieu because his solution does not require C++0x.)

  • 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-22T02:26:17+00:00Added an answer on May 22, 2026 at 2:26 am

    Without C++0x, it’s also possible to use chaining (I don’t recommend overloading the comma operator, the syntax gets weird).

    The syntax is somewhat different, but very close:

    CString const Path = AppendPath(base)(toplevel)(sub1)(filename);
    

    This is done simply by creating a temporary object that will perform the catenation through an overload of operator() and which will be implicitly convertible through operator CString() const.

    class AppenderPath
    {
    public:
      AppenderPath(){}
      AppenderPath(CString s): _stream(s) {}
    
      AppenderPath& operator()(CString const& rhs) {
        _stream += "/";
        _stream += rhs;
        return *this;
      }
    
      operator CString() const { return _stream; }
    
    private:
      CString _stream;
    };
    

    Then, you tweak AppendPath to return such an object:

    AppenderPath AppendPath(CString s) { return AppenderPath(s); }
    

    (Note, actually you could directly name it AppendPath)

    Making it generic as per @Martin’s suggestion:

    #include <iostream>
    #include <string>
    
    template <typename L, typename R>
    class Fold1l
    {
    public:
      typedef void (*Func)(L&, R const&);
    
      Fold1l(Func func, L l): _func(func), _acc(l) {}
    
      Fold1l& operator()(R const& r) { (*_func)(_acc, r); return *this; }
    
      operator L() const { return _acc; }
    
    private:
      Func _func;
      L _acc;
    };
    
    // U is just to foil argument deduction issue,
    // since we only want U to be convertible into a R
    template <typename R, typename L, typename U>
    Fold1l<R,L> fold1l(void (*func)(L&, R const&), U l) {
      return Fold1l<R,L>(func, l);
    }
    
    void AppendPath(std::string& path, std::string const& next) {
      path += "/"; path += next;
    }
    
    int main() {
      std::string const path = fold1l(AppendPath, "base")("next");
      std::cout << path << std::endl;
    }
    

    Code validated on ideone.

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

Sidebar

Related Questions

I have a helper function, which basically calls CompareTo on two objects, but does
The problem seems very strange. I have a AJAX helper function within a same
I have a helper assembly which includes a function to identify object types: namespace
I have this function: /** * Helper function that adds the values of b
I am using Symfony 1.3.2 on Ubuntu. I have written a little helper function
I have this function twitter_tweets_per_day($user, $rounding = 1) { // Helper function to calculate
I have this static helper function: public static DependencyObject GetParentObject(DependencyObject child) { if (child
I have defined my helper function in Helper: module CarsHelper def my_helper ... end
I use symfony 1.4.11 with doctrine. I have helper: function filterwords($text){ $filterWords=array ('some','filter','words'); $filterCount
I have a helper function which allows me to call functions in a different

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.