This is a c++ question.

Asked 2 years ago, Updated 2 years ago, 20 views

Hello.

I'm asking you this question because I don't understand something because I'm a C++ beginner.

#define CHECK(condition)                                             \
  do {                                                               \
    if (UNLIKELY(!(condition))) {                                 \
      Fatal(__FILE__, __LINE__, "Check failed: %s.", #condition); \
    }                                                                \
  } } while (0)

I'm looking at the sauce. How can I interpret it since the boom type is included in the condition part of CHECK, the condition is included, and the CHECK? CHECK (false) When used like this

do{
   if(UNLIKELY(!(false))) {                                 \
      Fatal(__FILE__, __LINE__, "Check failed: %s.", #condition); \
    }                                                                \
  } } while (0)

Do I have to interpret it like this way? And in the case of UNLIKELY, it is defined as follows.

# define UNLIKELY(condition) (condition)

Should I interpret it as UNLIKELY == (condition)? ex) Can I just replace UNLIKELY(false) with false?

And

#define DCHECK(condition)      ((void) 0)

How do I interpret a sentence like this? (void) What does zero mean?

There are so many things I don't know. I'd appreciate it if you let me know.

c++

2022-09-21 16:11

1 Answers

I don't know which API the code is from, but I'll rewrite the code you uploaded in a good way.

 
#define UNLIKELY(condition) (condition)

#define CHECK(condition) \
    do { \
        if (UNLIKELY(!(condition))) { \
            Fatal(FILE, LINE, "Check failed: %s.", #condition); \
        } \
    } while (0)

#define DCHECK(condition) ((void)0)

UNLIKELY() is used by CHECK(), so I raised it because it has to be declared before that.

And the role of ## in the #condition part of the macro function is to replace the values that are attached to it with strings.

Let me show you how the code is replaced by an example.

int main()
{
    // 1. Directly enter the bowl value
    CHECK(false);

    // 2. Enter conditional statements
    int a = 5;
    int b = 10;
    CHECK(a > b);

    return 0;
}
int main()
{
    // 1. Directly enter the bowl value
    do {
        if (!(false)) {
            Fatal(FILE, LINE, "Check failed: %s.", "false");
        }
    } while (0)

    // 2. Enter conditional statements
    int a = 5;
    int b = 10;
    do {
        if (!(a > b)) {
            Fatal(FILE, LINE, "Check failed: %s.", "a > b");
        }
    } while (0)

    return 0;
}

Now I'll explain why each macro function is made in that form.

 
// The case where a macro function that doesn't convert anything is used,
// There are two main types.
#define UNLIKELY(condition) (condition)

// 1. Just when you want to convey a meaning to a programmer on the code
//    - In other words, it is sometimes used in a similar sense to an annotation.

// 2. When used in conjunction with other macros
//    - For example, if you want to write a code that only runs when a define called __WIN32__ is declared,
#ifdef __WIN32__
    #define IF_WIN32_USE(condition) (condition)
#else
    #define IF_WIN32_USE(condition) ((void)0)
#endif
// It could be written in this way.
// That is, you want to use a macro with the same name in different situations.


// Next, the code below is a typical debugging checking macro.
// If the conditional statement to be substituted for condtion is false as a result, stop the program,
// Code to leave the log.
#define CHECK(condition) \
    do { \
        if (UNLIKELY(!(condition))) { \
            Fatal(FILE, LINE, "Check failed: %s.", #condition); \
        } \
    } while (0)
// #condition is the role of logging what the conditional statement was.
// If there is a code that puts the false value itself rather than the conditional statement, such as CHECK(false);
// That means that we will stop the program unconditionally at that location (while recording the log)


// Lastly, I already showed you the code below as an example.
// In other words, we're going to treat that code as something that doesn't exist in the program.
#define DCHECK(condition) ((void)0)

// For example, if you have a function and you want to use it only in certain situations,
void SomeFunction() { /*Do something complex*/}
#ifdef __WIN32__
    #define IF_WIN32_USE(condition) (condition)
#else
    #define IF_WIN32_USE(condition) ((void)0)
#endif
int main()
{
    IF_WIN32_USE(SomeFunction()); // If you write this, the function will only be called when __WIN32__ is declared, right?

    // If __WIN32__ is not declared, it will be replaced by the code below, but it will do nothing.
    (void)0; // This meaningless code is naturally removed when compiled.
}


2022-09-21 16:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.