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

The Archive Base Latest Questions

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

I have a function which takes three arguments void replace(const string, const string, string*)

  • 0

I have a function which takes three arguments

void replace(const string, const string, string*) ;

My problem is the second argument which I’d like to rewrite so it can be either a string or a Path object (from the Boost Filesystem library, but really any object that can be treated as a string should do) so I don’t have to convert paths to strings all over the place.

I know I can overload the function and make two implementations, that’s what I do now so I have two implementations of the same function:

void replace(const string, const string, string*) ;
void replace(const string, const path, string*) ;

But that doesn’t seem very elegant, even if the second one just converts the path to a string and calls the first (so not much code duplication). I’ve tried using templates instead, like this:

template <class T>
void replace(const string, const T, string*) ;

But the implementation of the function requires that it is a string, so if it is given a path it needs to be converted to a string within the function, which I’m not sure how to do or if it’s even a good thing to do.

To further complicate matters, in the future I might want to expand the function so it can also take a set of strings or a set of paths instead of just one string/path as the second argument.

What is the best way to handle all this? Is there no better way than having more than one implementation of the same function?

  • 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:17:02+00:00Added an answer on May 31, 2026 at 1:17 am

    Overloaded functions is the best approach for your use case.

    I usually go by the following rules:

    • If you want to perform same actions on different data types then prefer templates.

    • If you want to perform different actions on different data types then prefer function overloading.

    Your use case falls in the second category.

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

Sidebar

Related Questions

I have a ColdFusion function foo which takes three args, and the second two
I have a function which takes a custom struct as the argument. how can
I have a simple function which takes an array of characters as an argument,
Say I have a C function which takes a variable number of arguments: How
I have a function in MATLAB which takes another function as an argument. I
I want to have a SelectAll function which takes in a few arguments (class,
I have a function which takes as argument an std::istream& and writes a transformed
I have a function which takes in about 10 arguments, in which most of
I have a Python function which takes several arguments. Some of these arguments could
I have a function which takes four optional arguments: public function doSomething($x = null,

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.