The standard C++03 grammar for overloading an operator is as follows:
operator-function-id:
operator operator
operator operator < template-argument-list?>
The first one is the ordinary operator overloading syntax that we normally use, e.g.
Myclass operator + (Myclass s) {...}
But what does the second alternative mean? In particular, under what situation do we use the template-argument-list? After a quick look at C++11, I found that the second form was removed from the standard. What was the original intention of it?
EDIT: after testing with VC++2010, below is one way of using the above syntax, although it does not make much sense to me:
class K {
public:
int a;
template <int B>
int operator + (int b) {
return a+b+B;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
K k;
k.a=1;
int s;
s=k.operator+<115>(2);
printf("%d\n",s);
return 0;
}
output:118
When we overload the binary operator for user-defined types by using the code: obj3 = obj1 + obj2; The operator function is called using the obj1 object and obj2 is passed as an argument to the function.
Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type.
Syntax. Syntactically operators usually contrast to functions. In most languages, functions may be seen as a special form of prefix operator with fixed precedence level and associativity, often with compulsory parentheses e.g. Func(a) (or (Func a) in Lisp).
C does not support operator overloading (beyond what it built into the language).
The grammar rule which allows specialization of operator function templates still exists in C++11, it's just in a different place.
[temp.names]/1 (C++03):
A template specialization (14.7) can be referred to by a template-id:
template-id:
template-name < template-argument-listopt>
template-name:
identifier
template-argument-list:
template-argument template-argument-list , template-argument
template-argument:
assignment-expression type-id id-expression
[temp.names]/1 (C++11):
A template specialization (14.7) can be referred to by a template-id:
simple-template-id:
template-name < template-argument-listopt>
template-id:
simple-template-id operator-function-id < template-argument-listopt> <- HERE literal-operator-id < template-argument-listopt>
template-name:
identifer
template-argument-list:
template-argument ...opt template-argument-list , template-argument ...opt
template-argument:
constant-expression type-id id-expression
This was likely done because the grammatical rule operator-function-id is referred to in contexts where that template argument list would not make sense, so they moved the rule to somewhere more reasonable </conjecture>.
Here is an example of this rule in action:
struct foo{
template <typename T>
void operator() (T t) { std::cout << t; }
};
template <>
void foo::operator()<double> (double) {
std::cout << "It's a double!";
}
Note the specialization for operator()
for when T
is double
. If you run this code:
foo f;
f(0);
f(0.0);
Then 0
will be printed for the first call, and It's a double!
for the second.
Live demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With