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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:21:48+00:00 2026-06-14T21:21:48+00:00

I’m sorry this sounds like a common question, I couldn’t find the answer to

  • 0

I’m sorry this sounds like a common question, I couldn’t find the answer to my problem as far as I looked. The closest post would be this one: Template Specialization for basic POD only

Let’s say I have a class template <class T> class A {...};, and I want to overload operator+ as an internal binary operator (two objects of type A), and as a mixed binary operator (object of type A and numeric POD type).

Ideally, what I would like to write is:

#include <type_traits>
using namespace std;

// Declare/fine template
template <class T> class A {...};

// Internal binary operator
template < class T, class U >
    A< typename common_type<T,U>::type >
operator+ ( const A<T> &a, const A<U> &a ) { ... }

// Mixed binary operator
template < class T, class U >
    A< typename common_type<T,U>::type >
operator+ ( const A<T> &a, const U &b ) { ... }

But then it seems like the second definition is in conflict with the first one. Using the second definition, I know how to make sure U is a numeric POD type, that’s not the point. If I go that way, the problem is that I have no way of knowing what underlying template type is enclosed in U if it is some A.

Please tell me if my question is not clear enough, and thanks in advance! 🙂

EDIT: The template specification got wiped out by the HTML filter, in my last sentence “U if it is some A<T>“. In short, I’m saying T is hidden.

  • 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-14T21:21:49+00:00Added an answer on June 14, 2026 at 9:21 pm

    You can make it work with a little helper trait to distinguish specializations of A from more general types:

    #include <type_traits>
    
    
    // "A" template    
    
    template <typename> class A {};
    
    
    // Traits for "A-ness":
    
    template <typename> struct is_a : std::false_type { };
    template <typename T> struct is_a<A<T>> : std::true_type { };
    
    
    // Operators:
    
    template <class T, class U>
    A<typename std::common_type<T, U>::type>
    operator+(const A<T> & a, const A<U> & b);
    
    template <class T, class U,
              typename = typename std::enable_if<!is_a<U>::value>::type>
    A<typename std::common_type<T, U>::type>
    operator+(const A<T> & a, const U & b);
    

    This excludes the second overload from the viable set immediately, and so the problem of determining the return type of the second overload never comes up when only the first one is desired.

    (This is an example of using enable_if in defaulted template arguments to control the overload set.)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.