Special Interest Group on C++
sigcpp.bsky.social
Special Interest Group on C++
@sigcpp.bsky.social
Teaching, learning, and practicing contemporary C++

#cpp #cplusplus #education #teaching #EduSky

Moderated by @smurthys.bsky.social
Unwinding concept std::same_as up to std::integral_constant.

Compilation of possible implementations from cppreference. Hope seeing it all together at once helps appreciate the behind-the-scenes.

PS: read the code bottom to top; usable with no includes

sigcpp.godbolt.org/z/nx6Y3sfvo

#cpp #eduSky
November 20, 2024 at 5:53 PM
Delegate directly to the final ctor in a delegation chain, instead of delegating to a ctor which delegates to another, and so on. A bit more maintenance, but in general, can improves speed, size.

E.g., S1() involves 4 ctors; but S2() involves only 2.

sigcpp.godbolt.org/z/4e93ddd5x

#cpp #eduSky
November 20, 2024 at 5:53 PM
C, C++ allow declaring struct var together w struct type. The syntax looks odd but makes sense if seen alongside var declaration for a primitive type (1).

The syntax permits seemingly odd type qualifiers but the quals actually apply to the var not the type (2).

sigcpp.godbolt.org/z/fvY64MMqT #cpp
November 20, 2024 at 5:53 PM
Logan Smith illustrates the pitfalls of using C-style casts in C++ code.

Bottomline, instead of C-style cast, use an appropriate C++ cast such as `static_cast` and `dynamic_cast` depending on the situation.

www.youtube.com/watch?v=SmlL...

#cpp #cplusplus #programming #video
November 20, 2024 at 5:53 PM
Constants can help find certain errors at compile time instead of runtime: constexpr over const; const over non-const; macros/literals if you must.

You don't always get this benefit but strive.

E.g.: sigcpp.godbolt.org/z/sjd5vWrPn

(Avoid "naked new" but another time)

#cpp #cPlusPlus #programming
November 20, 2024 at 5:53 PM
If you need a std::string to be the reverse of another, you can:

1. Copy source and reverse the copy
or
2. Create a new string inited from reverse iterators on source

Approach 2 can be faster, and the new string can even be const.

sigcpp.godbolt.org/z/9coKnYTTs

#cpp #cplusplus #programming
November 20, 2024 at 5:53 PM
Use templates like these if you have many enum types and often convert them to integers. The 1-arg template converts an enum type to its underlying type.

PS: std::to_integer converts only from std::byte and the integer type must be explicit.

https://sigcpp.godbolt.org/z/4Waxaojrz

#cpp #cplusplus
November 20, 2024 at 5:53 PM