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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:02:30+00:00 2026-05-30T00:02:30+00:00

Is there a way to ensure the template overload isn’t selected unless all other

  • 0

Is there a way to ensure the template overload isn’t selected unless all other overloads fail, without resorting to enable_if?

Int should be handled by the long overload, but it’s being handled by the template overload, which the compiler doesn’t like.

class SetProxy {
public:
  void operator=(const TemplateString& value) {
    dict_.SetValue(variable_, value);
  }

  template<class T>
  void operator=(const T& value) {
    dict_.SetValue(variable_, TemplateString(value.data(), value.size()));
  }

  void operator=(long value) {
    dict_.SetIntValue(variable_, value);
  }
}
  • 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-30T00:02:31+00:00Added an answer on May 30, 2026 at 12:02 am

    The problem with the code in the question is that the template is the best match if an argument convertible to long or TemplateString is passed: the template doesn’t need to do a conversion while calling the other functions would involve a conversion.

    The desired behavior can be achieved even without the use of std::enable_if although I think prohibiting the use of std::enable_if is a made-up requirements: even if you can’t use C++2011 or Boost, it is extremely simple to implement std::enable_if. Implementing some of the desired type traits is a bit harder but doable. Restricting its use effectively means that you’ll need to implement essentially the same logic in a more or less contrived way which doesn’t say what it really does. For example, this code doesn’t use std::enable_if or SFINAE but there is an extra object actually be created which wouldn’t be needed if SFINAE had been used:

    #include <iostream>
    #include <string>
    
    class SetProxy {
        template <typename F>
        struct helper {
            helper(F const& v): value_(v) {}
            F const& value_;
        };
    
        template<typename F>
        void aux(helper<F> value, ...) {
            std::cout << "template " << value.value_ << "\n";
        }
        template<typename F>
        void aux(long value, int) {
            std::cout << "long: " << value << "\n";
        }
        template<typename F>
        void aux(std::string const& value, int) {
            std::cout << "string: " << value << "\n";
        }
    public:
        template<typename T>
        void operator=(const T& value) {
            this->aux<T>(value, 0);
        }
    };
    
    struct foo {};
    std::ostream& operator<< (std::ostream& out, foo const&) {
        return out << "foo";
    }
    
    int main()
    {
        SetProxy p;
        p = 17l;
        p = 17;
        p = foo();
        p = "hello";
        p = std::string("hello");
    }
    

    It doesn’t use the types from the original question because I don’t have them accessible and I didn’t feel like typing things irrelevant to the actual question. Note that this effectively contains the important bits of an implementation of std::is_convertible. Internally, it is necessary to forward to another template because the assignment operator cannot have a variable argument list: since there are viable conversions from int to helper<int>, there would be an ambiguity if there weren’t anything else to distinguish the types, a variable argument list is used to make the template version a bit worse.

    Just for reference, here is the in my opinion more readable version using std::enable_if:

    class SetProxy {
    public:
        template <typename T>
        typename std::enable_if<!std::is_convertible<T, long>::value
                                && !std::is_convertible<T, std::string>::value, void>::type
        operator= (T const& value) {
            std::cout << "template: " << value << "\n";
        }
        void operator= (long value) {
            std::cout << "long: " << value << "\n";
        }
        void operator= (std::string value) {
            std::cout << "std::string: '" << value << "'\n";
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to ensure that all WM_KEYDOWN events find their way into
Is there a way to ensure all created subprocess are dead at exit time
Is there a way to ensure all modules are cleared and freshly reloaded in
Is there any way to ensure that a class posts a particular NSNotification? (I
In the 1.6 API, is there a way to ensure that the onStart() method
Is there a way to test to see if javascript is enabled to ensure
When writing a print stylesheet, is there a way to ensure that an image
Is there a way to ensure that the box around a plot matches the
Is there a way to ensure, either through specifying order or by specifying dependencies,
Is there a way to ensure the $_POST data my code received came from

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.