Telegram Group & Telegram Channel
🎯 Как избежать макросов в C++ и остаться довольным

Сегодня я покажу вам, как можно избавиться от макросов в C++ и заменить их на более безопасные и выразительные конструкции.

🔴 Проблема: #define — это зло.
Они не уважают область видимости, не отлаживаются нормально, не подчиняются типам и могут вызвать кучу проблем, особенно в больших проектах.

👉 Вместо #define PI 3.14
Используем:

constexpr double PI = 3.14;


👉 Вместо #define SQUARE(x) ((x)*(x))
Используем шаблон:

template<typename T>
constexpr T square(T x) {
return x * x;
}


👉 Вместо #ifdef DEBUG ... #endif
Используем:

#ifdef DEBUG
inline constexpr bool is_debug = true;
#else
inline constexpr bool is_debug = false;
#endif

А дальше просто:

if constexpr (is_debug) {
std::cout << "Debug mode\n";
}


💡 constexpr, inline, template и if constexpr — это ваш новый арсенал для выразительного и безопасного кода без макросов.

➡️ @cpp_geek



tg-me.com/cpp_geek/296
Create:
Last Update:

🎯 Как избежать макросов в C++ и остаться довольным

Сегодня я покажу вам, как можно избавиться от макросов в C++ и заменить их на более безопасные и выразительные конструкции.

🔴 Проблема: #define — это зло.
Они не уважают область видимости, не отлаживаются нормально, не подчиняются типам и могут вызвать кучу проблем, особенно в больших проектах.

👉 Вместо #define PI 3.14
Используем:


constexpr double PI = 3.14;


👉 Вместо #define SQUARE(x) ((x)*(x))
Используем шаблон:

template<typename T>
constexpr T square(T x) {
return x * x;
}


👉 Вместо #ifdef DEBUG ... #endif
Используем:

#ifdef DEBUG
inline constexpr bool is_debug = true;
#else
inline constexpr bool is_debug = false;
#endif

А дальше просто:

if constexpr (is_debug) {
std::cout << "Debug mode\n";
}


💡 constexpr, inline, template и if constexpr — это ваш новый арсенал для выразительного и безопасного кода без макросов.

➡️ @cpp_geek

BY C++ geek




Share with your friend now:
tg-me.com/cpp_geek/296

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

Traders also expressed uncertainty about the situation with China Evergrande, as the indebted property company has not provided clarification about a key interest payment.In economic news, the Commerce Department reported an unexpected increase in U.S. new home sales in August.Crude oil prices climbed Friday and front-month WTI oil futures contracts saw gains for a fifth straight week amid tighter supplies. West Texas Intermediate Crude oil futures for November rose $0.68 or 0.9 percent at 73.98 a barrel. WTI Crude futures gained 2.8 percent for the week.

Why Telegram?

Telegram has no known backdoors and, even though it is come in for criticism for using proprietary encryption methods instead of open-source ones, those have yet to be compromised. While no messaging app can guarantee a 100% impermeable defense against determined attackers, Telegram is vulnerabilities are few and either theoretical or based on spoof files fooling users into actively enabling an attack.

telegram from us


Telegram C++ geek
FROM USA