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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:35:27+00:00 2026-06-09T13:35:27+00:00

C++ syntax allows defining overloaded operators either inside the struct/class like: struct X {

  • 0

C++ syntax allows defining overloaded operators either inside the struct/class like:

struct X
{
   void operator+(X);
}

or outside of the struct/class like:

void operator+(X, X);

but not as:

struct X
{
   static void operator+(X, X);
}

Does anybody know reasons for this decision? Why is the third form not allowed? (MSVC gives a syntax error.) Maybe there is some story behind this?

P.S. The presence of the first and second definitions at the same time creates ambiguity:

1>CppTest1.cxx
1>c:\ballerup\misc\cf_html\cpptest1.cxx(39) : error C2593: 'operator +' is ambiguous
1>        c:\ballerup\misc\cf_html\cpptest1.cxx(13): could be 'void B1::operator +(B1 &)'
1>        c:\ballerup\misc\cf_html\cpptest1.cxx(16): or       'void operator +(B1 &,B1 &)'
1>        while trying to match the argument list '(B1, B1)'

I do not understand why this ambiguity is any better than between 1,3 or 2,3.

  • 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-09T13:35:29+00:00Added an answer on June 9, 2026 at 1:35 pm

    I have no specific knowledge of any C++ discussion of this concept, so feel free to ignore this.

    But to me, you’ve got the question backwards. The question should be, “why would this syntax be allowed?”

    It provides no advantages at all over the current syntax. The non-static member function version has the same access to private members as your proposed static version. So if you need to access the privates to implement it, just make it a non-static member, exactly as you generally do with most members of a class.

    It doesn’t make it easier to implement asymmetric operators (ie: operator+(const X &x, const Y &y)). If you need private access to implement this, you’d still need a friend declaration for them in one of the classes.

    So I would say that the reason it doesn’t exist is that it isn’t necessary. Between non-member functions and non-static members, all of the necessary use cases are covered.


    Or, to put it another way:

    Free functions can do everything that the static function system can, and more.

    Through the use of free functions, you can get argument-dependent lookup happening for operators used in templates. You can’t do that with static functions, because those would have to be a member of a particular class. And you cannot add to a class from outside of the class, while you can add to a namespace. So if you need to put an operator in a particular namespace in order to make some ADL code work, you can. You can’t do that with static function operators.

    Thus, free functions are a superset of everything that your proposed static function system would provide. Since there is no benefit to allowing it, there is no reason to allow it, and therefore it is not allowed.


    which would make possible to use functors without instantiating them?

    That is a contradiction in terms. A “functor” is a “function object”. A type is not an object; therefore, it cannot be a functor. It can be a type that, when instantiated, will result in a functor. But the type alone will not be a functor.

    Furthermore, being able to declare Typename::operator() static would not mean that Typename() would do what you want. That syntax already has an actual meaning: instantiate a Typename temporary by calling the default constructor.

    Lastly, even if all that weren’t the case, what good would that actually be? Most template functions that take a callable of some type work just as well with a function pointer as with a functor. Why would you want to restrict your interface, not merely to just functors, but to functors which cannot have internal data? That means you wouldn’t be able to pass capturing lambdas and so forth.

    What good is a functor that cannot possibly contain state? Why do you want to force the user into passing “functors” that don’t have state? And why do you want to prevent the user from being able to use lambdas?

    So your question is derived from a false assumption: even if we had it, it wouldn’t give you what you want.

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

Sidebar

Related Questions

I am using a platform which uses a JavaScript-like syntax and allows to convert
Google's query syntax allows to search phrases like as * as a skyscraper where
Maybe I'm stupid but Rails provides this nifty syntax for generating URL's like so:
The Visual Studio autoexp.dat syntax allows you to display ‘the name of the most-derived
C# has syntax that allows you to specify the argument index in a string
Ruby syntax reference says about case statements: Comparisons are done by operator === infact,
Rake allows for the following syntax: task :my_task, :arg1, :arg2 do |t, args| puts
The delta import syntax for the Solr 1.4 data import handler allows for up
Is there a column comment syntax that allows me to specify a column comment
I'm used to regular expression syntax which allows for named matches from the search

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.