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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:11:52+00:00 2026-06-06T00:11:52+00:00

I have a templated class for volume objects where operator+= is implemented as a

  • 0

I have a templated class for volume objects where operator+= is implemented as a member function and the operator+is implemented as non-member functions:

template <typename Class>
Class operator+(Class c1, const Class& c2) {
   return c1 += c2;
}

// Add a constant to every element in the volume
template <typename Class, typename dataType>
Class operator+(Class c, dataType constant) {
   return c += constant;
}

template <typename Class, typename dataType>
Class operator+(dataType constant, Class c) {
   return c += constant;
}

Then I try to compile the following:

volume + 1.3;

where volume is a derived type from the templated volume class.
This gives me the following error:

error: ambiguous overload for ‘operator+’ in ‘volume + 1.3’

Why is the call ambiguous?

  • 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-06T00:11:53+00:00Added an answer on June 6, 2026 at 12:11 am

    Your second template could be inferred with Class = typeof(volume) and dataType = double, or your third template can be inferred with dataType = typeof(volume) and Class = double. The compiler can’t choose between them, even though quite possibly the third template would fail to instantiate.

    I’m assuming that volume has a user-defined type. If it has a built-in type, then I don’t think the call would be ambiguous, because for the purpose of operator overload resolution only, there are “real” functions double operator+(double, double); etc, that would be selected before the templates are even considered.

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

Sidebar

Related Questions

I have a templated class with an templated member function template<class T> class A
I have a templated class like this: template <typename T> class AguiEvent { std::vector<std::tr1::function<void(T,
I have the following templated function... template< class T > T *create_object( lua_State *L
I have a template class with a variadic template member function that I am
I have a templated class template <typename Data> class C { ..... } In
I have a problem regarding 'static const' member initialization. In a templated class I
Say I have a templated class like template <typename T> struct Node { //
I have the following setup: A templated class SpecialModel: template<typename A, typename B> class
lets say i have a templated class template <typename T> struct Widget { //generalized
I have the following templated class structure struct TraitA{}; struct TraitB{}; template<typename trait> struct

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.