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

How To Find Channels On Telegram?

There are multiple ways you can search for Telegram channels. One of the methods is really logical and you should all know it by now. We’re talking about using Telegram’s native search option. Make sure to download Telegram from the official website or update it to the latest version, using this link. Once you’ve installed Telegram, you can simply open the app and use the search bar. Tap on the magnifier icon and search for a channel that might interest you (e.g. Marvel comics). Even though this is the easiest method for searching Telegram channels, it isn’t the best one. This method is limited because it shows you only a couple of results per search.

Telegram Be The Next Best SPAC

I have no inside knowledge of a potential stock listing of the popular anti-Whatsapp messaging app, Telegram. But I know this much, judging by most people I talk to, especially crypto investors, if Telegram ever went public, people would gobble it up. I know I would. I’m waiting for it. So is Sergei Sergienko, who claims he owns $800,000 of Telegram’s pre-initial coin offering (ICO) tokens. “If Telegram does a SPAC IPO, there would be demand for this issue. It would probably outstrip the interest we saw during the ICO. Why? Because as of right now Telegram looks like a liberal application that can accept anyone - right after WhatsApp and others have turn on the censorship,” he says.

telegram from vn


Telegram C++ geek
FROM USA