Skip to main content
added 3 characters in body
Source Link
Laci
  • 2.8k
  • 1
  • 14
  • 24

UPDATE4: As answeredpointed out by @Alex Bakanov you can't use #ifdef while expanding macros, so there is no single line solution for your question which works in all cases. Nevertheless, I hope the idea I wrote here may be useful.

if you use #define FOO 1 or #define FOO 0, combination of #define and if constexpr can be used. Note that it gives an error if FOO is not defined. This program gives 1 as result:

#include<iostream>

#define FOO 1
#define MY_IFDEF(x,y) if constexpr (x) y;

int main()
{
    int bar =0;
    MY_IFDEF(FOO,bar++)

    std::cout << bar << "\n";  
}

UPDATE: Based on @eerorika's comment to avoid the error if FOO is not defined, the following declaration has to be added:

constexpr bool FOO = false;

UPDATE2: This version works in any circumstances, the only question is that is it worth the effort?

#ifdef FOO
    constexpr bool USED_FOO = true;
#else
    constexpr bool USED_FOO = false;
#endif

#define MY_IFDEF(x,y) if constexpr (USED_##x) y ;

UPDATE3: C compatible version. Note that in this case in theory it is evaluated runtime not compile time, but the compiler will realize that it is always true/false and generates the code accordingly:

#ifdef FOO
    const static bool USED_FOO = true;
#else
    const static bool USED_FOO = false;
#endif

#define MY_IFDEF(x,y) if (USED_##x) y;

UPDATE4: As answered by @Alex Bakanov you can't use #ifdef while expanding macros, so there is no single line solution for your question which works in all cases. Nevertheless, I hope the idea I wrote here may be useful.

if you use #define FOO 1 or #define FOO 0, combination of #define and if constexpr can be used. Note that it gives an error if FOO is not defined. This program gives 1 as result:

#include<iostream>

#define FOO 1
#define MY_IFDEF(x,y) if constexpr (x) y;

int main()
{
    int bar =0;
    MY_IFDEF(FOO,bar++)

    std::cout << bar << "\n";  
}

UPDATE: Based on @eerorika's comment to avoid the error if FOO is not defined, the following declaration has to be added:

constexpr bool FOO = false;

UPDATE2: This version works in any circumstances, the only question is that is it worth the effort?

#ifdef FOO
    constexpr bool USED_FOO = true;
#else
    constexpr bool USED_FOO = false;
#endif

#define MY_IFDEF(x,y) if constexpr (USED_##x) y ;

UPDATE3: C compatible version. Note that in this case in theory it is evaluated runtime not compile time, but the compiler will realize that it is always true/false and generates the code accordingly:

#ifdef FOO
    const static bool USED_FOO = true;
#else
    const static bool USED_FOO = false;
#endif

#define MY_IFDEF(x,y) if (USED_##x) y;

UPDATE4: As pointed out by @Alex Bakanov you can't use #ifdef while expanding macros, so there is no single line solution for your question which works in all cases. Nevertheless, I hope the idea I wrote here may be useful.

if you use #define FOO 1 or #define FOO 0, combination of #define and if constexpr can be used. Note that it gives an error if FOO is not defined. This program gives 1 as result:

#include<iostream>

#define FOO 1
#define MY_IFDEF(x,y) if constexpr (x) y;

int main()
{
    int bar =0;
    MY_IFDEF(FOO,bar++)

    std::cout << bar << "\n";  
}

UPDATE: Based on @eerorika's comment to avoid the error if FOO is not defined, the following declaration has to be added:

constexpr bool FOO = false;

UPDATE2: This version works in any circumstances, the only question is that is it worth the effort?

#ifdef FOO
    constexpr bool USED_FOO = true;
#else
    constexpr bool USED_FOO = false;
#endif

#define MY_IFDEF(x,y) if constexpr (USED_##x) y ;

UPDATE3: C compatible version. Note that in this case in theory it is evaluated runtime not compile time, but the compiler will realize that it is always true/false and generates the code accordingly:

#ifdef FOO
    const static bool USED_FOO = true;
#else
    const static bool USED_FOO = false;
#endif

#define MY_IFDEF(x,y) if (USED_##x) y;
added 1 character in body
Source Link
Laci
  • 2.8k
  • 1
  • 14
  • 24

UPDATE4: As answered by @Alex Bakanov you can't use #ifdef while expanding macros, so there is no single line solution for your question which works in all cases. Nevertheless, I hope the idea I wrote here may be useful.

if you use #define FOO 1 or #define FOO 0, combination of #define and if constexpr can be used. Note that it gives an error if FOO is not defined. This program gives 1 as result:

#include<iostream>

#define FOO 1
#define MY_IFDEF(x,y) if constexpr (x) y;

int main()
{
    int bar =0;
    MY_IFDEF(FOO,bar++)

    std::cout << bar << "\n";  
}

UPDATE: Based on @eerorika's comment to avoid the error if FOO is not defined, the following declaration has to be added:

constexpr bool FOO = false;

UPDATE2: This version works in any circumstances, the only question is that is it worth the effort?

#ifdef FOO
    constexpr bool USED_FOO = true;
#else
    constexpr bool USED_FOO = false;
#endif

#define MY_IFDEF(x,y) if constexpr (USED_##x) y ;

UPDATE3: C compatible version. Note that in this case in theory it is evaluated runtime not compile time, but the compiler will realize that it is always true/false and generates the code accordingly:

#ifdef FOO
    const static bool USED_FOO = true;
#else
    const static bool USED_FOO = false;
#endif

#define MY_IFDEF(x,y) if (USED_##x) y;

UPDATE4: As answered by @Alex Bakanov you can't use #ifdef while expanding macros, so there is no single line solution for your question which works in all cases. Nevertheless I hope the idea I wrote here may be useful.

if you use #define FOO 1 or #define FOO 0, combination of #define and if constexpr can be used. Note that it gives an error if FOO is not defined. This program gives 1 as result:

#include<iostream>

#define FOO 1
#define MY_IFDEF(x,y) if constexpr (x) y;

int main()
{
    int bar =0;
    MY_IFDEF(FOO,bar++)

    std::cout << bar << "\n";  
}

UPDATE: Based on @eerorika's comment to avoid the error if FOO is not defined, the following declaration has to be added:

constexpr bool FOO = false;

UPDATE2: This version works in any circumstances, the only question is that is it worth the effort?

#ifdef FOO
    constexpr bool USED_FOO = true;
#else
    constexpr bool USED_FOO = false;
#endif

#define MY_IFDEF(x,y) if constexpr (USED_##x) y ;

UPDATE3: C compatible version. Note that in this case in theory it is evaluated runtime not compile time, but the compiler will realize that it is always true/false and generates the code accordingly:

#ifdef FOO
    const static bool USED_FOO = true;
#else
    const static bool USED_FOO = false;
#endif

#define MY_IFDEF(x,y) if (USED_##x) y;

UPDATE4: As answered by @Alex Bakanov you can't use #ifdef while expanding macros, so there is no single line solution for your question which works in all cases. Nevertheless, I hope the idea I wrote here may be useful.

if you use #define FOO 1 or #define FOO 0, combination of #define and if constexpr can be used. Note that it gives an error if FOO is not defined. This program gives 1 as result:

#include<iostream>

#define FOO 1
#define MY_IFDEF(x,y) if constexpr (x) y;

int main()
{
    int bar =0;
    MY_IFDEF(FOO,bar++)

    std::cout << bar << "\n";  
}

UPDATE: Based on @eerorika's comment to avoid the error if FOO is not defined, the following declaration has to be added:

constexpr bool FOO = false;

UPDATE2: This version works in any circumstances, the only question is that is it worth the effort?

#ifdef FOO
    constexpr bool USED_FOO = true;
#else
    constexpr bool USED_FOO = false;
#endif

#define MY_IFDEF(x,y) if constexpr (USED_##x) y ;

UPDATE3: C compatible version. Note that in this case in theory it is evaluated runtime not compile time, but the compiler will realize that it is always true/false and generates the code accordingly:

#ifdef FOO
    const static bool USED_FOO = true;
#else
    const static bool USED_FOO = false;
#endif

#define MY_IFDEF(x,y) if (USED_##x) y;
added 24 characters in body
Source Link
Laci
  • 2.8k
  • 1
  • 14
  • 24

UPDATE4: As answered by @Alex Bakanov you can't use #ifdef while expanding macros, so there is no single line solution for your question which works in all cases. Nevertheless I hope the idea I wrote here may be useful.

if you use #define FOO 1 or #define FOO 0, combination of #define and if constexpr can be used. Note that it gives an error if FOO is not defined. This program gives 1 as result:

#include<iostream>

#define FOO 1
#define MY_IFDEF(x,y) if constexpr (x) y;

int main()
{
    int bar =0;
    MY_IFDEF(FOO,bar++)

    std::cout << bar << "\n";  
}

UPDATE: Based on @eerorika's comment to avoid the error if FOO is not defined, the following declaration has to be added:

constexpr bool FOO = false;

UPDATE2: This version works in any circumstances, the only question is that is it worth the effort?

#ifdef FOO
    constexpr bool _FOOUSED_FOO = true;
#else
    constexpr bool _FOOUSED_FOO = false;
#endif

#define MY_IFDEF(x,y) if constexpr (_##xUSED_##x) y ;

UPDATE3: C compatible version. Note that in this case in theory it is evaluated runtime not compile time, but the compiler will realize that it is always true/false and generates the code accordingly:

#ifdef FOO
    const static bool _FOOUSED_FOO = true;
#else
    const static bool _FOOUSED_FOO = false;
#endif

#define MY_IFDEF(x,y) if (_##xUSED_##x) y;

UPDATE4: As answered by @Alex Bakanov you can't use #ifdef while expanding macros, so there is no single line solution for your question which works in all cases. Nevertheless I hope the idea I wrote here may be useful.

if you use #define FOO 1 or #define FOO 0, combination of #define and if constexpr can be used. Note that it gives an error if FOO is not defined. This program gives 1 as result:

#include<iostream>

#define FOO 1
#define MY_IFDEF(x,y) if constexpr (x) y;

int main()
{
    int bar =0;
    MY_IFDEF(FOO,bar++)

    std::cout << bar << "\n";  
}

UPDATE: Based on @eerorika's comment to avoid the error if FOO is not defined, the following declaration has to be added:

constexpr bool FOO = false;

UPDATE2: This version works in any circumstances, the only question is that is it worth the effort?

#ifdef FOO
    constexpr bool _FOO = true;
#else
    constexpr bool _FOO = false;
#endif

#define MY_IFDEF(x,y) if constexpr (_##x) y ;

UPDATE3: C compatible version. Note that in this case in theory it is evaluated runtime not compile time, but the compiler will realize that it is always true/false and generates the code accordingly:

#ifdef FOO
    const static bool _FOO = true;
#else
    const static bool _FOO = false;
#endif

#define MY_IFDEF(x,y) if (_##x) y;

UPDATE4: As answered by @Alex Bakanov you can't use #ifdef while expanding macros, so there is no single line solution for your question which works in all cases. Nevertheless I hope the idea I wrote here may be useful.

if you use #define FOO 1 or #define FOO 0, combination of #define and if constexpr can be used. Note that it gives an error if FOO is not defined. This program gives 1 as result:

#include<iostream>

#define FOO 1
#define MY_IFDEF(x,y) if constexpr (x) y;

int main()
{
    int bar =0;
    MY_IFDEF(FOO,bar++)

    std::cout << bar << "\n";  
}

UPDATE: Based on @eerorika's comment to avoid the error if FOO is not defined, the following declaration has to be added:

constexpr bool FOO = false;

UPDATE2: This version works in any circumstances, the only question is that is it worth the effort?

#ifdef FOO
    constexpr bool USED_FOO = true;
#else
    constexpr bool USED_FOO = false;
#endif

#define MY_IFDEF(x,y) if constexpr (USED_##x) y ;

UPDATE3: C compatible version. Note that in this case in theory it is evaluated runtime not compile time, but the compiler will realize that it is always true/false and generates the code accordingly:

#ifdef FOO
    const static bool USED_FOO = true;
#else
    const static bool USED_FOO = false;
#endif

#define MY_IFDEF(x,y) if (USED_##x) y;
added 224 characters in body
Source Link
Laci
  • 2.8k
  • 1
  • 14
  • 24
Loading
deleted 4 characters in body
Source Link
Laci
  • 2.8k
  • 1
  • 14
  • 24
Loading
added 371 characters in body
Source Link
Laci
  • 2.8k
  • 1
  • 14
  • 24
Loading
added 77 characters in body
Source Link
Laci
  • 2.8k
  • 1
  • 14
  • 24
Loading
added 363 characters in body
Source Link
Laci
  • 2.8k
  • 1
  • 14
  • 24
Loading
added 85 characters in body
Source Link
Laci
  • 2.8k
  • 1
  • 14
  • 24
Loading
deleted 4 characters in body
Source Link
Laci
  • 2.8k
  • 1
  • 14
  • 24
Loading
added 30 characters in body
Source Link
Laci
  • 2.8k
  • 1
  • 14
  • 24
Loading
Source Link
Laci
  • 2.8k
  • 1
  • 14
  • 24
Loading