Sign in

Special Interest Group on C++

@sigcpp.bsky.social
126 followers 56 following 29 posts

Teaching, learning, and practicing contemporary C++ #cpp #cplusplus #education #teaching #EduSky Moderated by @smurthys.bsky.social

PostsRepliesMedia
Special Interest Group on C++ @sigcpp.bsky.social · 31/05/2024
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
Syntax-highlighted C++ code in a dark theme:

template<class T, T v>
struct integral_constant
{
    static constexpr T value = v;
    using value_type = T;
    using type = integral_constant;
    constexpr operator value_type() const noexcept { return value; }
    constexpr value_type operator()() const noexcept { return value; }
};

using true_type = integral_constant<bool, true>;
using false_type = integral_constant<bool, false>;

template<class T, class U>
struct is_same : false_type {};
 
template<class T>
struct is_same<T, T> : true_type {};

template<class T, class U>
inline constexpr bool is_same_v = is_same<T, U>::value;

template<class T, class U>
concept SameHelper = is_same_v<T, U>;
 
template<class T, class U>
concept same_as = SameHelper<T, U> && SameHelper<U, T>;
101
Special Interest Group on C++ @sigcpp.bsky.social · 17/05/2024
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
Syntax-highlighted C++ code in a dark theme:

struct S1 {
    int i_, j_, k_;

    S1() : S1(1) {}  //start delegation chain
    S1(int i) : S1(i, 2) {}
    S1(int i, int j) : S1(i, j, 3) {}
    S1(int i, int j, int k) : i_(i), j_(j), k_(k) {}
};Syntax-highlighted C++ code in a dark theme:

struct S2 {
    int i_, j_, k_;

    S2() : S2(1, 2, 3) {}  //delegate to terminal ctor 
    S2(int i) : S2(i, 2, 3) {}
    S2(int i, int j) : S2(i, j, 3) {}
    S2(int i, int j, int k) : i_(i), j_(j), k_(k) {}
};
001
Special Interest Group on C++ @sigcpp.bsky.social · 15/05/2024
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
Syntax-highlighted C++ code in a dark theme:

int i; //int is a type, i is of type int
struct S { } s; //S is a type, s is of type SSyntax-highlighted C++ code in a dark theme:

struct A {
    int i = 1;
} a1;

const struct B {
    int i = 2;
} b1; //b1 is const

constexpr struct C {
    int i = 3;
} c1; //c1 is constexpr

static struct D {
    int i = 4;
} d1; //d1 is static

static const struct E {
    int i = 5;
} e1; //e1 is static const
001
Special Interest Group on C++ @sigcpp.bsky.social · 26/11/2023
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
White text on black:

And so it was, that the C-style cast,

the most powerful of all the casts,
with the easiest and most tempting syntax,
who gleefully casts away your const,
who insidiously breaks your pointer casts,
and who laughs at the light of private inheritance,

was therefore the scariest cast of all.
001
Special Interest Group on C++ @sigcpp.bsky.social · 11/09/2023
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
C++ code in a dark theme:

auto n_var = SIZE_MAX;
auto p1 = new char[n_var]; //runtime errorC++ code in a dark theme:

const auto n_const = SIZE_MAX; 
auto p2 = new char[n_const]; //compile error

constexpr auto n_constexpr = SIZE_MAX; 
auto p3 = new char[n_constexpr]; //compile error

auto p4 = new char[SIZE_MAX]; //compile error
011
Special Interest Group on C++ @sigcpp.bsky.social · 16/07/2023
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
Syntax-highlighted C++ code in a dark theme:

//source string to reverse 
const std::string s1 = "hello";

// Approach 1: new string cannot be const
std::string s2(s1);
std::reverse(s2.begin(), s2.end());

// Approach 2: new string can even be const
const std::string s3(s1.rbegin(), s1.rend());
101
Special Interest Group on C++ @sigcpp.bsky.social · 06/07/2023
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. sigcpp.godbolt.org/z/4Waxaojrz #cpp #cplusplus
Syntax-highlighted C++ code:

template <std::integral I, typename E>
requires std::is_enum_v<E>
constexpr I to_integer(E e) noexcept
{
    return I(e);
}

template <typename E>
requires std::is_enum_v<E>
constexpr auto to_integer(E e) noexcept
{
    return std::underlying_type_t<E>(e);
}
120