Блог пользователя vkditya0997

Автор vkditya0997, история, 9 лет назад, По-английски

Hello Everyone,

What's the difference in these two macros ?

#define LL long long int 
   and 
typedef long long LL;

and

What's the difference between these ?

inline void myFunction(){}
   and 
void myFunction(){}

Thanks !

  • Проголосовать: нравится
  • -6
  • Проголосовать: не нравится

»
9 лет назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

Auto comment: topic has been updated by vkditya0997 (previous revision, new revision, compare).

»
9 лет назад, # |
  Проголосовать: нравится +18 Проголосовать: не нравится

For the first one look here. define is low-level preprocessing construct which will blindly replace all LL tokens with long long int regardless of their meaning before compiler even gets a chance to take a look at the code. typedef works on compiler level, it tells compiler to make a new type called LL and make it same as long long, so it won't screw up some other places (see the link for a good example).

For the second one: here. Actually, there is little difference: inline suggests that compiler inline that function wherever it's called instead of pushing arguments to stack and making a jump, can save some time. But it's still only suggestion, compiler is free to do anything it wants: both discard inline modifier and inline function without that modifier. There is some serious difference on linking stage, but that does not matter in competitive programming.

»
9 лет назад, # |
  Проголосовать: нравится +32 Проголосовать: не нравится

Inline? Check this vs this.

»
9 лет назад, # |
  Проголосовать: нравится +20 Проголосовать: не нравится

One clear practical difference is that names introduced by #define cannot be used in other contexts in the program. For example:

#define LL long long int 
void foo(void) {
  int LL = 13; // <- compilation error here
}
  • »
    »
    9 лет назад, # ^ |
      Проголосовать: нравится +22 Проголосовать: не нравится

    Wow, you made my day. Until now I thought that this would not compile with typedef either, but all this C++ namespace and scope stuff can sometimes be ridiculous.

    The code below compiles successfully (at least by g++4-8):

    #include <iostream>
    typedef long long i64;
    
    int main() {
        int i64 = 1;
        std::cout << (::i64)i64 << std::endl;
    }
    
    • »
      »
      »
      9 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится +11 Проголосовать: не нравится

      Standard name lookup rules apply to your example. It compiles for the same reason why the next example compiles — because the inner scope is always looked up first unless the :: operator is used:

      int i = 1;
      int main() {
        long i = 2;
        cout << i << " " << ::i << endl;
      }
      

      And the other way around: trying to define a variable and a type in the same scope with the same name leads to an error.

      typedef long long i64;
      int i64 = 1; // <- error