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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:58:59+00:00 2026-05-27T06:58:59+00:00

I’ve a templatized class template <typename T> class Factors { public: Factors(){}; deque<string> stringsDeck;

  • 0

I’ve a templatized class

template <typename T>
class Factors
{
    public:
    Factors(){};
    deque<string> stringsDeck;

    // some methods and variables here
   map <string, string> getNext();
};

The getNext method combines a string used as key with the strings from stringsDeck used as value and returns a map <string,string>.
Provided that I have templatized stringify and string2num functions, I would like to have a method map<string,Scalar> getNext() which for every other type except from string acts converting the values of the map to the specified template type T.

The compiler doesn’t let me overload two methods with the same name but with different return type, specifically:

map <string, T > getNext()
{
// converts the values of getNext() return map into the specified type T
return convertedMapWithValuesOfTypeT;
}

What can be the solution in this case? I would like to keep the name of the method the same for string and other types (basically numerical types which can be converted from string via a lexical cast)

  • 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-27T06:58:59+00:00Added an answer on May 27, 2026 at 6:58 am

    The language doesn’t allow function overloads which differ only on
    return type, because in most cases, the return type isn’t considered in
    function overload resolution. In this case, you really have three
    possible solutions:

    • The simplest (and therefore the preferred solution in most cases) is
      just to give the two functions different names:
      getNextAsMapToType and
      getNextAsMapToString, for example.
    • Alternatively, you could declare the function a template:
      template<typename U>
      std::map<string, U> getNext();
      

      then, specialize this function for std::string and
      T (and for
      nothing else). The user will have to specify
      getNext<std::string>()
      or getNext<...> to call the one he wants.
      Generally, I would find this considerably less readable than the
      previous solution, but it might be applicable in templates, where
      the names should contain or at least suggest the name of the type.

    • Finally, it _is_ possible to simulate overloading on the return
      type, if you don’t mind some extra complexity. To do this, you still
      have to implement one of the other solutions, but the client code
      doesn’t see it. Basically, your getNext() function must return a
      proxy, with overloaded conversion functions, something like:

      class Proxy
      {
          Factors* myOwner;
      public:
          Proxy( Factors& owner ) : myOwner( &owner ) {}
          operator std::map<std::string, std::string>() const
          {
              return myOwner->getNextAsMapToString();
          }
          operator std::map<std::string, T>() const
          {
              return myOwner->getNextAsMapToType();
          }
      };
      
      Proxy getNext() { return Proxy( *this ); }
      

      Client code can then just call getNext(), and depending on what they
      do with the results, either getNextAsMapToString or
      getNextAsMapToType will be called.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.