-3

Problem Description

I participate in competitive programming. Given that I am in desperate need of an essential template to memorize, I use macros very much.

For instance, I use

#define large long long
large value;

There are two operators that I am annoyed of though: << and >> as of input and output.

Expectation

I would like to see something like

#include <iostream>
using namespace std;
#define and <<
#define with >>
#define print cout
#define input cin

int main() {
    long long value1, value2;
    input value1 with value2;
    print value1 and value2;
}

I expect this program to echo two integral values that the user inputs.

Issue

However, I constantly receive an error stating that Cannot #define something as an operator. How do I resolve that? If I cannot, is there a replacement to #define and <<? I absolutely appreciate your response.

江赫霆
  • 9
  • 5
  • 17
    Wouldn't it be easier to just learn the actual language? You're not even saving yourself any keystrokes by replacing the actual names with longer names. – Nathan Pierson Jun 20 '22 at 05:30
  • 10
    The idea of renaming those operators is horrendous. Besides, `and` is a reserved word. The point of competitive programming is to save time and write terrible code so you're halfway there already with terrible code. You just need to work on the save-time part now, so why would you not use better names like `ll` for `long long` etc? I don't see how `input` and `print` save you time, and they are certainly less readable to anyone familiar with C++. Besides, most competitive programming tasks will require the use of `std::getline`, not so much the stream operators. – paddy Jun 20 '22 at 05:31
  • 9
    Creating clever macros to redefine the language, add syntactic sugar, or to make it more cute is an anti-pattern that all enthusiastic programmers new to C/C++ try at least once. In practice, it doesn't achieve much and only makes the code harder to maintain. Here's a guiding principal: Try to program without ever using your own `#define` statements unless it's absolutely necessary. You'll thank yourself later for adopting this personal coding standard. – selbie Jun 20 '22 at 05:35
  • 1
    Defining custom operators doesn’t save time; any time you save by typing fewer characters will be spent again (with interest) when you have try to debug or understand the obfuscated code later on – Jeremy Friesner Jun 20 '22 at 05:42
  • you'll have a fun time when the error messages don't display "and" or "with" but the actual operator – Raildex Jun 20 '22 at 05:53
  • There are no literals here, let alone 'normal literals', whatever they may be. – user207421 Jun 20 '22 at 06:18
  • In addition to the word `and`, here is a list of the names used by the standard library, which you are not allowed to `#define` (if you include *any* standard header). http://eel.is/c++draft/libraryindex#A – BoP Jun 20 '22 at 07:43
  • 1
    As others stated, `and` is a reserved keyword, so you can't redefine it. But even it weren't, `input value1 with value2;` would resolve to `cin value1 >> value2;` and `print value1 and value2;` would resolve to `cout value1 << value2;`, neither of which make sense. – Remy Lebeau Jun 20 '22 at 08:02
  • 1
    Replace `#define large long long` with `using large = long long;`. The preprocessor is The Devil™. Do not dance with The Devil if you don't know the tune. – Eljay Jun 20 '22 at 11:33
  • Hello everyone, from your responses, I have understood that macros are not essential to programming. Thank you very much for your replies. I will close this problem now. – 江赫霆 Jul 12 '22 at 04:00

1 Answers1

5

It is perfectly legal (though nauseous) to have a macro that expands to an operator such as << or >>, example. You just can't use and as the name of your macro, since and is a keyword.

So if you insist on going this route, just choose some other name that isn't on the list of keywords.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • 1
    Or other [reserved identifiers](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – Nathan Pierson Jun 20 '22 at 05:38
  • `put` and `get` are probably not good examples, because they are used by the standard library as well (e.g. `std::get` and `std::basic_ostream::put`). Per https://www.eel.is/c++draft/library#macro.names-1 defining them as macro also causes undefined behavior if any standard library header is included. Of course practically speaking it is only a problem if a standard library header is included _after_ the macro definition. – user17732522 Jun 20 '22 at 05:43
  • This is perfect for my problem. Thank you very much! – 江赫霆 Jun 21 '22 at 00:25