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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:07:26+00:00 2026-06-13T10:07:26+00:00

I have a function that is overloaded for many types. But my current problem

  • 0

I have a function that is overloaded for many types.
But my current problem is due to that (LWS here : lws) :

#include <iostream>
#include <string>
#include <sstream>
#include <type_traits>

// First version
template<typename T, class = typename std::enable_if<std::is_fundamental<T>::value>::type> 
std::string f(const T& x)
{
    return std::to_string(x);
}

// Second version
template<typename... T> 
std::string f(const std::tuple<T...>& x)
{
    return std::to_string(sizeof...(T)); // It's just an example here
}

// Third version
template<typename T, class = typename std::enable_if<!std::is_fundamental<T>::value>::type, class = void> 
std::string f(const T& x)
{
    std::ostringstream oss;
    oss<<x;
    return oss.str();
}

// Main
int main(int argc, char* argv[])
{
   std::cout<<f(42)<<std::endl;
   std::cout<<f(std::string("Hello World"))<<std::endl;
   std::cout<<f(std::tuple<int, int, int, int, int, int>(4, 8, 15, 16, 23, 42))<<std::endl;
   return 0;
}

My problem is that when we call f() for a std::tuple, the third version is executed and not the second one.

How to solve this problem (a solution would be to allow the third version only for types where << is defined, but I don’t know how to do that, and if this is the best way to solve the problem) ?

  • 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-06-13T10:07:28+00:00Added an answer on June 13, 2026 at 10:07 am

    Your only problem is that you forgot to #include <tuple>, which is shown in the error on LWS. Here’s a fixed version that compiles correctly. The second overload is always a better match than the third one since it’s more specialized according to partial ordering rules.

    If you still want to know how to check for a functions existence that triggers SFINAE if it’s not there, check this answer of mine on a question dedicated to that topic. 🙂 It even has an example that pretty much matches what you want.

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

Sidebar

Related Questions

Suppose I have a function that has a parameter that is overloaded by many
I have a bunch of overloaded functions that operate on certain data types such
So I have function that formats a date to coerce to given enum DateType{CURRENT,
I have a function that takes an input string and then runs the string
I have a function that I'm trying to optimize at the moment but I'm
I have a overloaded function that works correctly. (f in the example). When I
I have a function that processes a given vector, but may also create such
Inside my Controller i have function that runs after user clicks on item, which
In my Java code I have function that gets file from the client in
I have function Start() that is fired on ready. When I click on .ExampleClick

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.