c++ - Infix vs prefix syntax: name lookup differences -
operators in c++ considered alternative syntax functions/methods, in context of overloading. if so, 2 expressions below should synonymous:
std::cout << 42; operator<<(std::cout, 42);
in practise, second statement leads following error:
call of overloaded ‘operator<<(std::ostream&, int)’ ambiguous
as usual, such error message accompanied list of possible candidates, these are:
operator<<(basic_ostream<_chart, _traits>& __out, char __c) operator<<(basic_ostream<char, _traits>& __out, char __c) operator<<(basic_ostream<char, _traits>& __out, signed char __c) operator<<(basic_ostream<char, _traits>& __out, unsigned char __c)
such error raises @ least 2 questions:
- in way 2 statements different (in terms of name lookup)?
- why
operator<<(basic_ostream<char, _traits>& __out,
int
__c)
missing?
it seems, infix , prefix notations not interchangeable -- different syntax entails different name resolution tactics. differences , did come from?
no, 2 expressions should not synonymous. std::cout << 42
looked both operator<<(std::cout, 42)
, std::cout.operator<<(42)
. both lookups produce viable candidates, second 1 better match.
Comments
Post a Comment