Warning: mkdir(): No space left on device in /var/www/tgoop/post.php on line 37

Warning: file_put_contents(aCache/aDaily/post/cpp_geek/--): Failed to open stream: No such file or directory in /var/www/tgoop/post.php on line 50
C++ geek@cpp_geek P.323
CPP_GEEK Telegram 323
Пару фишек про шаблоны, которые могут спасти час дебага:

1. CTAD (Class Template Argument Deduction, C++17)
Не надо вручную указывать аргументы:


std::pair p(42, 3.14); // вместо std::pair<int, double> p(42, 3.14);
std::vector v = {1,2,3}; // компилятор сам выведет std::vector<int>


Помогает сократить код и избежать опечаток.

2. Fold-выражения (C++17) для арг-паков:


auto sum = [](auto... args){
return (args + ...); // ((a + b) + c) + ...
};
std::cout << sum(1,2,3,4); // 10


Позволяют писать операции над любым числом параметров без рекурсии.

3. SFINAE → Concepts (C++20)
Старый стиль через enable_if легко сломать:


template<class T>
std::enable_if_t<std::is_integral_v<T>, T>
foo(T x) { return x*2; }


С Concepts чище и понятнее:


template<std::integral T>
T foo(T x) { return x*2; }


4. CRTP (Static polymorphism)
Быстрее виртуалок и без RTTI:


template<class D>
struct Base {
void interface() { static_cast<D*>(this)->impl(); }
};
struct Derived : Base<Derived> {
void impl() { std::cout<<"OK\n"; }
};


Шаблоны — это не только про универсальность, но и про ясность кода. Освой тонкости, и они станут 🔧, а не головняком.

➡️ @cpp_geek



tgoop.com/cpp_geek/323
Create:
Last Update:

Пару фишек про шаблоны, которые могут спасти час дебага:

1. CTAD (Class Template Argument Deduction, C++17)
Не надо вручную указывать аргументы:


std::pair p(42, 3.14); // вместо std::pair<int, double> p(42, 3.14);
std::vector v = {1,2,3}; // компилятор сам выведет std::vector<int>


Помогает сократить код и избежать опечаток.

2. Fold-выражения (C++17) для арг-паков:


auto sum = [](auto... args){
return (args + ...); // ((a + b) + c) + ...
};
std::cout << sum(1,2,3,4); // 10


Позволяют писать операции над любым числом параметров без рекурсии.

3. SFINAE → Concepts (C++20)
Старый стиль через enable_if легко сломать:


template<class T>
std::enable_if_t<std::is_integral_v<T>, T>
foo(T x) { return x*2; }


С Concepts чище и понятнее:


template<std::integral T>
T foo(T x) { return x*2; }


4. CRTP (Static polymorphism)
Быстрее виртуалок и без RTTI:


template<class D>
struct Base {
void interface() { static_cast<D*>(this)->impl(); }
};
struct Derived : Base<Derived> {
void impl() { std::cout<<"OK\n"; }
};


Шаблоны — это не только про универсальность, но и про ясность кода. Освой тонкости, и они станут 🔧, а не головняком.

➡️ @cpp_geek

BY C++ geek


Share with your friend now:
tgoop.com/cpp_geek/323

View MORE
Open in Telegram


Telegram News

Date: |

Developing social channels based on exchanging a single message isn’t exactly new, of course. Back in 2014, the “Yo” app was launched with the sole purpose of enabling users to send each other the greeting “Yo.” The initiatives announced by Perekopsky include monitoring the content in groups. According to the executive, posts identified as lacking context or as containing false information will be flagged as a potential source of disinformation. The content is then forwarded to Telegram's fact-checking channels for analysis and subsequent publication of verified information. In the “Bear Market Screaming Therapy Group” on Telegram, members are only allowed to post voice notes of themselves screaming. Anything else will result in an instant ban from the group, which currently has about 75 members. There have been several contributions to the group with members posting voice notes of screaming, yelling, groaning, and wailing in different rhythms and pitches. Calling out the “degenerate” community or the crypto obsessives that engage in high-risk trading, Co-founder of NFT renting protocol Rentable World emiliano.eth shared this group on his Twitter. He wrote: “hey degen, are you stressed? Just let it out all out. Voice only tg channel for screaming”. Hui said the time period and nature of some offences “overlapped” and thus their prison terms could be served concurrently. The judge ordered Ng to be jailed for a total of six years and six months.
from us


Telegram C++ geek
FROM American