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

Export WhatsApp stickers to Telegram on iPhone

You can’t. What you can do, though, is use WhatsApp’s and Telegram’s web platforms to transfer stickers. It’s easy, but might take a while.Open WhatsApp in your browser, find a sticker you like in a chat, and right-click on it to save it as an image. The file won’t be a picture, though—it’s a webpage and will have a .webp extension. Don’t be scared, this is the way. Repeat this step to save as many stickers as you want.Then, open Telegram in your browser and go into your Saved messages chat. Just as you’d share a file with a friend, click the Share file button on the bottom left of the chat window (it looks like a dog-eared paper), and select the .webp files you downloaded. Click Open and you’ll see your stickers in your Saved messages chat. This is now your sticker depository. To use them, forward them as you would a message from one chat to the other: by clicking or long-pressing on the sticker, and then choosing Forward.

Spiking bond yields driving sharp losses in tech stocks

A spike in interest rates since the start of the year has accelerated a rotation out of high-growth technology stocks and into value stocks poised to benefit from a reopening of the economy. The Nasdaq has fallen more than 10% over the past month as the Dow has soared to record highs, with a spike in the 10-year US Treasury yield acting as the main catalyst. It recently surged to a cycle high of more than 1.60% after starting the year below 1%. But according to Jim Paulsen, the Leuthold Group's chief investment strategist, rising interest rates do not represent a long-term threat to the stock market. Paulsen expects the 10-year yield to cross 2% by the end of the year. A spike in interest rates and its impact on the stock market depends on the economic backdrop, according to Paulsen. Rising interest rates amid a strengthening economy "may prove no challenge at all for stocks," Paulsen said.

telegram from tw


Telegram C++ geek
FROM USA