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: |

What is Telegram Possible Future Strategies?

Cryptoassets enthusiasts use this application for their trade activities, and they may make donations for this cause.If somehow Telegram do run out of money to sustain themselves they will probably introduce some features that will not hinder the rudimentary principle of Telegram but provide users with enhanced and enriched experience. This could be similar to features where characters can be customized in a game which directly do not affect the in-game strategies but add to the experience.

Tata Power whose core business is to generate, transmit and distribute electricity has made no money to investors in the last one decade. That is a big blunder considering it is one of the largest power generation companies in the country. One of the reasons is the company's huge debt levels which stood at ₹43,559 crore at the end of March 2021 compared to the company’s market capitalisation of ₹44,447 crore.

telegram from br


Telegram C++ geek
FROM USA