1

I am not able to give seperate meaning to ^ using #define, like #define ^ +. But where as I am able to give meaning to $ using #define , like #define $ +.

Could you please let me know how $ is different from ^?
On which rule of c++ standard this ^ is not allowed?

I am using VC++ 2012, Not tried with GCC or any other tool.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Susanth
  • 29
  • 2
  • 1
    `^` is bitwise xor already whereas `$` is.. nothing. do operator overloading with `^` if you want to give it special meaning – user Mar 12 '17 at 09:22
  • 1
    http://stackoverflow.com/questions/369495/what-are-the-valid-characters-for-macro-names – deviantfan Mar 12 '17 at 09:26
  • 1
    https://stackoverflow.com/questions/21884985/will-using-a-preprocessor-directive-to-define-what-a-dollar-sign-represents-caus – Ry- Mar 12 '17 at 09:27

3 Answers3

4

^ is an operator in C++. It is a bitwise XOR. Please read https://www.tutorialspoint.com/cplusplus/cpp_operators.htm

Operators cannot be "redefined" in this way, you need to overload them. C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively (https://www.tutorialspoint.com/cplusplus/cpp_overloading.htm and https://en.wikibooks.org/wiki/C%2B%2B_Programming/Operators/Operator_Overloading#Bitwise_operators)

For bitwise operators, generally, they have a lower precedence than the arithmetic operators, so if ^ were to be overloaded for exponentiation, x ^ y + z may not work as expected.

For XOR the canonical form is: Type operator^(const Type &lhs, const Type &rhs); // Bitwise exclusive or, while for member function versions: Type &operator^=(const Type &rhs); // Assign exclusive or.

reference: http://articles.emptycrate.com/2009/10/12/nobody_understands_c_part_8_operator_overloading.html

Dev2017
  • 857
  • 9
  • 31
  • If ^ is an operator and this is the reason for this failure, then the same should happen for +,=,* and /. But all the following are valid, no compilation error. #define + -, #define * - etc. – Susanth Mar 13 '17 at 04:44
  • If you really have a C preprocessor which lets you `#define` operator symbols, then you are definitely well distant from standard C++ – rici Mar 13 '17 at 07:07
3

The rules for names of preprocessor macros are the same as for identifiers: they can contain uppercase and lowercase letters, underscores, and numeric digits only. The first character in an identifier cannot be a digit.

That means it is not possible to use #define to redefine the meaning of operators like ^.

In standard C++, this also excludes identifiers containing a $. However, some compilers support identifiers containing $ as an extension.

Peter
  • 35,646
  • 4
  • 32
  • 74
  • "In standard C++, this also excludes identifiers containing a $." is it mentioned in any part of the C++ specification? – Susanth Mar 13 '17 at 04:47
  • @susanth: Sure. It's in section 2.10, where identifiers are defined as starting with a one 53 characters (upper and lower case ASCII letters, plus the `_` symbol) and continuing with more of the same, plus possibly the 10 ASCII digits. They may also include "universal-character-names", which are unicode characters encoded in hex signalled with `\u` or `\U`, but only letter-like characters are allowed, and the smallest valid code is `\u00A8`. (And the compiler may allow you to use the actual Unicode character.) 16.3 paragraph 10 says `#define` must be followed by an identifier. – rici Mar 13 '17 at 07:16
-2

^ is an operator you can use it to get input of strings with space like this scanf ("%[^\n]s,&x)...So you can only overload it and not define it as it has already been defined