Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do identifier rules apply for operator overloading function?

Tags:

c++

According to the C++ identifier naming rules:

A valid identifier is a sequence of one or more letters, digits, or underscore characters (_) and must start either with a letter or underscore. Spaces, punctuation marks, and symbols cannot be part of an identifier.

But the name of a function for operator overloading can contain characters other than a letter, digit or underscore, like for example:

user_data_type operator+(const user_data_type & t) const;

Is this an exception to the the identifier naming rules or does function names used for operator overloading not considered as an identifier?

like image 553
stackerjoe Avatar asked May 15 '16 15:05

stackerjoe


People also ask

Which of the following is true about operator overloading?

Which is the correct statement about operator overloading? Explanation: Both arithmetic and non-arithmetic operators can be overloaded. The precedence and associativity of operators remains the same after and before operator overloading. 10.

What is not true about the operator overloading?

8. Which of the following statements is NOT valid about operator overloading? Explanation: The overloaded operator must not have at least one operand of its class type. Explanation: Operator overloading is the way adding operation to the existing operators.


1 Answers

operator+ is not a normal identifier, it is an operator-function-id as defined in 13.5/1 in N4140. By definition, it consists of the word operator followed by an operator, the + in your example. By this rule, you can also write

operator +

or even

user_data_type
operator


+
(const user_data_type & t) const;

as again, operator+ is not a normal identifier.

The rule you cite does not apply here.

like image 114
Baum mit Augen Avatar answered Oct 25 '22 22:10

Baum mit Augen