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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:16:29+00:00 2026-06-13T20:16:29+00:00

I am providing a library that supports a function bar(). What it does when

  • 0

I am providing a library that supports a function bar(). What it does when you pass in a scalar value (like a double, int, whatever) is different from what happens if you pass in something that is not a scalar value (in all expected cases, a user-defined type). So I wrote code like this:

#include <iostream>

class Foo
{
public:
   template <class T> void bar(T const &rhs) { std::cout << "T" << std::endl; }
   void bar(double rhs) { std::cout << "double" << std::endl; }
};

int main()
{
   Foo foo;
   foo.bar(4);
}

The problem with this is on the second line of main(). The result of this code is output of “T”. The compiler prefers the template over the call to bar(double), and I am assuming this is because the parameter is an int, which it would rather cast to int const& (since a const& can reference an r-value).

My question is “is there a way I can support every scalar value without explicitly calling them out?” I really don’t want to call out every possible type, because… well… there’s a lot of them. I would have to cover everything from char to long long, include every combination of volatile and unsigned, and so forth.

I know that just changing the 4 to a 4.0 works, but this is for the public interface to a library, and requiring the user to type 4.0 instead of 4 is just dirty.

  • 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-13T20:16:30+00:00Added an answer on June 13, 2026 at 8:16 pm

    Yes, with traits:

    #include <type_traits>
    #include <iostream>
    
    class Foo
    {
    public:
       template <class T>
       typename std::enable_if<!std::is_scalar<T>::value, void>::type bar(T const & rhs)
       {
          std::cout << "T" << std::endl;
       }
    
       void bar(double rhs)
       {
          std::cout << "double" << std::endl;
       }
    };
    

    There are six basic categories of types: scalars, functions, arrays, classes, unions and references. And void. Each of them have a corresponding trait. See here for more details.

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

Sidebar

Related Questions

Does anyone know if there's a Flash (ActionScript) library for providing back button support?
I have a Data Access Layer library that I would like to make portable.
I have to use an extern library providing lots of free functions that do
I'm looking for a terminal UI library providing widgets like buttons, checkboxes and so
I would like to use the mms-computing.co.uk.device.twain.jar library in a Java project that should
I am writing a C++/Cli interop layer that sits between a native library providing
I know that Javassist is a Java library providing a means to manipulate the
My DBA has several SSIS packages that he would like the functionality of providing
Lets say I have a library function that I cannot change that produces an
i just discovered http://code.google.com/p/re2 , a promising library that uses a long-neglected way (

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.