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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:33:35+00:00 2026-06-14T10:33:35+00:00

Is it possible to specialize a function (or a class at least) to select

  • 0

Is it possible to specialize a function (or a class at least) to select between constant (compile-time!) integer and all other arguments? And if so, it would be nice to specialize (enable_if) for specific constant values only.

In the example below this would mean output “var”, “const”, and “var”, not three “var”s.

#include <type_traits>
#include <iostream>
using namespace std;

struct test
{
    template <typename T>
    test& operator=(const T& var) { cout << "var" << endl; return *this; }
    template <int n> // enable_if< n == 0 >
    test& operator=(int x) { cout << "const" << endl; return *this; }
};

int main()
{
    test x;
    x = "x";
    x = 1;
    int y = 55;
    x = y;
    return 0;
}

UPDATE: edited the code to emphasize that it has to be compile-time constant.

  • 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-14T10:33:36+00:00Added an answer on June 14, 2026 at 10:33 am

    To get var, const, var in your example you can do that.

    struct test {
      template <typename T>
      test& operator=(const T& var) { cout << "var" << endl; return *this; }
      test& operator=(int &&x) { cout << "const" << endl; return *this; }
    };
    

    It will work for all temporaries, but it will fail for:

    const int yy = 55;
    x = yy;
    

    To make it work for such a case too you will need to add:

    test& operator=(int &x)       { cout << "var" << endl; return *this; }
    test& operator=(const int &x) { cout << "const" << endl; return *this; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It is possible to specialize some class member function outside the template definition: template<class
Is it possible to specialize this template for any basic_string's? template<class T> struct X
I want to specialize following member function: class foo { template<typename T> T get()
Is it possible to call a function defined in a non-specialised template class from
Possible Duplicate: Specialization of templated member function in templated class template <class T> class
Is it possible to specialize a templatized method for enums? Something like (the invalid
Possible Duplicate: How to serialize an IList<T>? I wish to serialize an class (let's
In C++, I am trying to specialize a templated function for a object that
Base class MessageHandler has derived classes. They would like to pass messages to each
Please refer to the below code Specialized function in non specialized Template class Is

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.