Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it legal to use #elif with #ifdef?

A simple question that Google doesn't help me with. Is it legal in C++ to use #elif clause in the context of #ifdef? It seems to compile and work as expected with all the major compilers in the c++11 mode (MSVC 2015/2017, clang, GCC), but I'm not certain whether it is standard-compliant.

like image 213
Violet Giraffe Avatar asked Oct 31 '17 22:10

Violet Giraffe


People also ask

Is using a VPN legal?

Recap. Unless you're doing anything illegal, using a VPN in the U.S. is perfectly legally sound. However, if you're using a VPN in a country where it's not legal, it's important to download one of the best VPNs in terms of privacy.

Is it legal to use a VPN for Netflix?

Is It Legal to Use a VPN With Netflix? At the time of this writing, the US has no laws that make it illegal to use a VPN with Netflix. So although some people equate the use of a VPN to piracy, that's not the case at all. Piracy would be if you downloaded copyrighted material illegally.

Can you get caught using a VPN?

If a VPN has IP leaks, DNS leaks, or WebRTC leaks, this could result in your online habits being tracked by your ISP. It will also allow online services and websites to detect your actual IP address. As a result, a leaky VPN is the easiest way to get caught out.

Will VPNs be banned?

It isn't illegal to stream movies from legitimate streaming services while using a VPN (If VPNs are legal where you are). However, due to licensing issues, many streaming platforms like Netflix frown on this. In fact, some service providers will do their best to detect and block VPN users.


2 Answers

Yes, the grammar allows an #elif after preceding, matching #if, #ifdef or #ifndef:

if-section:
    if-group elif-groupsoptelse-groupoptendif-line

if-group:
    # if constant-expression new-line groupopt
    # ifdef identifier new-line groupopt
    # ifndef identifier new-line groupopt

Note that #ifdef(X) is just short for #if defined(X), and #ifndef(X) for #if ! defined(X).

like image 171
Kerrek SB Avatar answered Sep 28 '22 05:09

Kerrek SB


For me, the most important point on this question is actually rosshjb's comment under the question:

@RemyLebeau Yes, we can use #ifdef with #elif. But, If we #define macro with value 0 for #ifdef case, the #ifdef case tests it to true. Otherwise, if we #define macro with value 0 for #elif case, the #elif case test it to false. – rosshjb Jan 19 '20 at 19:40

So if you have a block like:

#ifdef __linux__
  <some Linux code here>
#elif _WIN32
  <some Windows code here>
#endif

Then the second test is significantly different from the first - the first is checking whether __linux__ is defined at all, where the second is checking that the symbol _WIN32 evaluates to true. For many cases it will behave the same, but it is not guaranteed to do so.

The full equivalent is actually:

#ifdef __linux__
  <some Linux code here>
#elif defined(_WIN32)
  <some Windows code here>
#endif

Which is probably not obvious to everyone.

Using Kerrick SB's answer, you can also write the same #if statement as:

#if defined(__linux__)
  <some Linux code here>
#elif defined(_WIN32)
  <some Windows code here>
#endif

Which makes it more obvious that the defined is the same for both the #if and the #elif

like image 34
Michael Firth Avatar answered Sep 28 '22 06:09

Michael Firth