0

So I was recently looking at competitive programming, and many people have the following code in their program to make it faster to type:

#define ll long long

For every instance of ll, long long will be replaced. However, I also saw another similar thing:

using ll = long long

Is there any difference between using one or the other, and when should I use which?

  • 1
    The `ll` macro is a token substitution, which is applied at the token level. If you have `void foo(int ll);` it'll get substituted to `void foo(int long long);`. Not terribly different than if you had used `sed` to do a search-and-replace. The `using` version honors scope, and is a type alias rather than a token substitution, and is part of C++ core language rather than the C preprocessor. In the particular use case cited in the question, I'd recommend using neither. But I don't do competitive programming. – Eljay Dec 23 '20 at 04:12
  • 2
    `using` is limited in scope, which means you can deliberately constrain it as needed. A macro works by text substitution, which means its scope is unlimited (after the macro is defined) and that can cause surprises. More generally, however, using a name like `ll` for `long long` is an extremely bad habit. The fact it is regularly used on competitive programming sites is evidence that such sites are not useful for learning good technique. – Peter Dec 23 '20 at 04:13
  • 1
    never use a macro in C++ if you have an alternative. – Eugene Dec 23 '20 at 04:58
  • The duplicate targets have `typedef` instead of `using`, but these two are similar. Related: [What is the difference between 'typedef' and 'using' in C++11?](https://stackoverflow.com/q/10747810/9716597) – L. F. Dec 23 '20 at 11:37

0 Answers0