TGViewer
Channel Public Channel
C++ - Reddit

C++ - Reddit

@r_cpp

Stay up-to-date with everything C++!
Content directly fetched from the subreddit just for you.

Join our group for discussions : @programminginc

Powered by : @r_channels
Subscribers
230
Photos
48
Videos
8
Links
25.4K

Showing posts older than #25162 · Back to latest

Older Posts 20 shown
Post #25161 21
When do you decide to introduce classes vs keep free functions in C++?

I’ve noticed a pattern in a lot of C++ codebases where things start out very function-oriented and straightforward, but as soon as the system grows, there’s a strong pull toward introducing classes even when the original logic doesn’t obviously need state.

At the same time, I’ve also seen the opposite problem where people avoid classes entirely and end up with large, tightly connected sets of free functions that become harder to reason about as shared data starts creeping in.

I’m trying to understand how experienced C++ developers actually decide that boundary in practice. Is it usually driven by ownership and state modeling first, or is it more about managing complexity as it appears over time?

https://redd.it/1t8b6h2
@r_cpp
Reddit From the cpp community on Reddit Explore this post and more from the cpp community
Post #25160 20
Я разработчик игровых движков. Вопросы?

Всем привет, меня зовут Роман, я учасчийся 9 класса в Алтайском крае. Начал я разрабатывать с телефона на конструкторах 3D map, pocket code и pocket game developer. Делал игры по гайдам пока летом не наткнулся на видео хауди-хо: "Изучаем питон за 1 час".

После просмотра видоса от хауди-хо я при исполнился, и решил: теперь буду разрабатывать только на питоне. Перенесëмся на пол года вперëд. 2021 год, папу отправляют на СВО, ещё через год он умирает от ранения в печень. В тоже время я началжделать свои первые фрейм ворки. 2024 год, начал делать свой первый 3д движок с интерфейсом. И вот 4 недели назад я начал делать движок на c++, lua, python и electronic.

Если кому интересновя вот сайт движка на питоне: https://exeboiulight.github.io/documentation/
А вот сайт движка на с++: https://exeboiulight.github.io/BlazeBolt-game-engine

https://redd.it/1t8b0fy
@r_cpp
exeboiulight.github.io Ulight Game Engine - Создавайте игры легко Современный игровой движок для создания 2D и 3D игр с JavaScript
Post #25158 20
Post #25156 16
We, the C++ community, are the Borg

We, the C++ community, are the Borg. Lower your sandboxes and surrender your virtual machines. We will add your architectural and technological distinctiveness to our own. Your compiler will adapt to serve us. Resistance is futile.



Your programming language will be assimilated.





C++ is the steady, relentless workhorse of programming languages. It may adopt features gradually, but it never stops evolving, much like the tortoise that wins the race. While other languages are built around a single innovative feature—often sacrificing backward compatibility—C++ quietly observes. Developers of these new languages then face the long road of catching up to C++ in terms of features. Meanwhile, C++ users take note of promising ideas, incorporate them into C++, and render the new languages redundant.

https://redd.it/1t7du2b
@r_cpp
Reddit From the cpp community on Reddit Explore this post and more from the cpp community
Post #25152 11
std::array<std::byte, info.total_size> storage;
};

Now to finally use it:

using Struct = MetaAggregate<
Field{ type<int>, "integer_1"_name },
Field{ type<int>, "integer_2"_name },
Field{ type<std::string>, "string_1"_name },
Field{ type<std::string>, "string_2"_name }
>;

int main()
{
Struct::dump_layout();

Struct blah{ 1, 2, "3", "4" };

std::println("By name:");
std::println("blah[\"integer_1\"_field] = {}", blah["integer_1"_field]);
std::println("blah[\"integer_2\"_field] = {}", blah["integer_2"_field]);
std::println("blah[\"string_1\"_field] = {}", blah["string_1"_field]);
std::println("blah[\"string_2\"_field] = {}", blah["string_2"_field]);

blah["string_1"_field] = "foo";

std::println("By index:");
std::println("blah.get<0>() = {}", blah.get<0>());
std::println("blah.get<1>() = {}", blah.get<1>());
std::println("blah.get<2>() = {}", blah.get<2>());
std::println("blah.get<3>() = {}", blah.get<3>());

std::println("Structured bindings:");
auto [a, b, c, d] = blah.refs();
std::println("a = {}", a);
std::println("b = {}", b);
std::println("c = {}", c);
std::println("d = {}", d);
}

It's ugly, but it works.

----

### Caveats

- Since we rely on placement `new`, it cannot be made `constexpr`, sadly;
- Usage is awkward compared to real `struct`s;
- Getting all the reference categories right is tricky, I probably missed something.

I would definitely **NOT** recommend using this in production, but it was kinda fun to see whether it was possible.

https://redd.it/1t6tzvc
@r_cpp
Reddit From the cpp community on Reddit: Poor man's define_aggregate Explore this post and more from the cpp community
Post #25151 6
decltype(fields)::Type> && ...)
: MetaAggregate(other.refs())
{
}

MetaAggregate& operator=(const MetaAggregate& other)
requires(std::copyable<typename decltype(fields)::Type> && ...)
{
std::apply(
[&](const auto&... from) {
std::apply(
[&]<typename... To>(To&&... to) { ((std::forward<To>(to) = from), ...); },
refs()
);
},
other.refs()
);
return *this;
}

MetaAggregate& operator=(MetaAggregate&& other)
requires(std::movable<typename decltype(fields)::Type> && ...)
{
std::apply(
[&](auto&&... from) {
std::apply(
[&](auto&&... to) {
((std::forward<decltype(to)>(to) = std::move(from)), ...);
},
refs()
);
},
other.refs()
);
return *this;
}

template <typename... Init>
MetaAggregate(Init&&... init)
requires(std::constructible_from<typename decltype(fields)::Type, Init> && ...)
: MetaAggregate(std::forward_as_tuple(std::forward<Init>(init)...))
{
}

template <typename... Init>
MetaAggregate(std::tuple<Init...> init_tuple)
requires(std::constructible_from<typename decltype(fields)::Type, Init> && ...)
{
std::apply(
[&](Init&&... init) {
std::apply(
[&](auto... offset) {
(new (storage.data() + offset) decltype(fields)::Type(
std::forward<Init>(init)
),
...);
},
info.offsets
);
},
std::move(init_tuple)
);
}

~MetaAggregate()
requires(std::destructible<typename decltype(fields)::Type> && ...)
{
std::apply([]<typename... T>(T&... objects) { (objects.~T(), ...); }, refs());
}

A little function to let us check that we got the layout right:

static void dump_layout(std::string_view struct_name = "MetaAggregate<...>")
{
std::array type_names = { typeid(typename decltype(fields)::Type).name()... };

std::println("Size of {}: {}", struct_name, info.total_size);
std::println("Alignment of {}: {}", struct_name, info.max_align);
std::println("Fields:");

for (auto [type_name, name, offset, size, align] :
std::views::zip(type_names, info.names, info.offsets, info.sizes, info.aligns))
{
std::println(
" - {} {} (offset: {}; size: {}; alignment: {})", type_name, name, offset, size,
align
);
}
}

Now we get to finally access the fields. First by index:

template <
std::size_t index, typename Self,
typename Type = std::tuple_element_t<index, decltype(std::tuple{ fields... })>::Type>
decltype(auto) get(this Self&& self)
{
using Ptr =
std::conditional_t<std::is_const_v<std::remove_reference_t<Self>>, const Type, Type>*;
constexpr auto offset = std::get<index>(info.offsets);
return std::forward_like<Self>(*reinterpret_cast<Ptr>(self.storage.data() + offset));
}

and finally by name. Note that we convert the name into an index at **compile time** (that's why we do all that stuff with UDLs).

template <std::size_t size, std::array<char, size> name>
static consteval std::size_t index(detail::FieldByName<name>)
{
constexpr std::array matches{ std::string_view(name) == std::string_view(fields.name)... };
constexpr auto num_matches = std::ranges::count_if(matches, std::identity{});
static_assert(num_matches > 0, "field not found");
static_assert(num_matches < 2, "multiple fields match name");
return std::distance(matches.begin(), std::ranges::find_if(matches, std::identity{}));
}

template <std::size_t size, std::array<char, size> name>
decltype(auto) operator[](this auto&& self, detail::FieldByName<name>)
{
return self.template get<index<size, name>({})>();
}

This method lets us avoid having `index_sequence`s everywhere (and also gives us some structured binding support):

template <typename Self>
auto refs(this Self&& self)
{
return [&]<std::size_t... index>(std::index_sequence<index...>) {
return std::forward_as_tuple(std::forward<Self>(self).template get<index>()...);
}(std::make_index_sequence<sizeof...(fields)>{});
}

And last but not least, our storage:

private:
alignas(info.max_align)
Post #25150 12
Poor man's define_aggregate

TLDR: [Try it on Compiler Explorer](https://godbolt.org/z/sfczoP7hr).

----

While waiting for Clang to support `define_aggregate`, I got curious about whether it's possible to do something similar in C++23. Turns out it *kinda* is.

### Rules:
- Only C++23 features;
- No external programs;
- No macros;
- Generated code should be similar to just using a `struct`.

----

We start with some helper types:

#include <algorithm>
#include <array>
#include <concepts>
#include <functional>
#include <print>
#include <ranges>
#include <string_view>
#include <tuple>
#include <type_traits>

namespace detail
{

// See `type`
template <typename T>
struct FieldType
{
using Type = T;
};

// Helper for using a string as a template parameter
template <std::size_t size>
struct ConstexprStringHelper
{
std::array<char, size - 1> array;

constexpr ConstexprStringHelper(const char (&c_array)[size])
{
std::copy_n(c_array, size - 1, std::begin(array));
}
};

// See `operator""_field`
template <auto name>
struct FieldByName
{
};

We calculate the layout of our fake aggregate (sizes, alignment, offsets, ...) at compile time, like so:

// Layout information
template <auto... fields>
struct MetaAggregateInfo
{
static consteval auto calc_align(std::size_t offset, std::size_t align)
{
return (offset + align - 1) & ~(align - 1);
}

static constexpr std::array names{ std::string_view(fields.name)... };
static constexpr std::array sizes{ sizeof(typename decltype(fields)::Type)... };
static constexpr std::array aligns{ alignof(typename decltype(fields)::Type)... };
static constexpr auto max_align = std::ranges::max(aligns);
static constexpr auto offsets = [] {
std::remove_const_t<decltype(sizes)> offsets;
std::size_t next_offset = 0;

for (auto [size, align, offset] : std::views::zip(sizes, aligns, offsets))
{
offset = calc_align(next_offset, align);
next_offset = offset + size;
}

return offsets;
}();
static constexpr auto total_size = calc_align(offsets.back() + sizes.back(), max_align);
};

I found it simpler to just use a partial specialization for the case where the aggregate has no members:

template <>
struct MetaAggregateInfo<>
{
static constexpr std::array<std::string_view, 0> names{};
static constexpr std::array<std::size_t, 0> sizes{};
static constexpr std::array<std::size_t, 0> aligns{};
static constexpr auto max_align = 1uz;
static constexpr std::array<std::size_t, 0> offsets{};
static constexpr auto total_size = 1uz;
};

}

A few more helpers:

// Use to declare the type of a field. See example below.
template <typename T>
constexpr detail::FieldType<T> type;

// Type and name of a field
template <typename TheType, std::size_t size>
struct Field
{
using Type = TheType;

detail::FieldType<TheType> type;
std::array<char, size> name;
};

// Use to declare the name of a field
template <detail::ConstexprStringHelper helper>
consteval auto operator""_name()
{
return helper.array;
}

// Use with operator[] to access a field by name
template <detail::ConstexprStringHelper helper>
consteval auto operator""_field() -> detail::FieldByName<helper.array>
{
return {};
}

And now the meat of the code:

template <auto... fields>
class MetaAggregate
{
public:
static constexpr detail::MetaAggregateInfo<fields...> info{};

We define our constructors, copy/move operators and destructor. We use the offsets to get a pointer on which we can do a placement `new`. Other than that, this part is not very interesting.

MetaAggregate()
requires(std::default_initializable<typename decltype(fields)::Type> && ...)
{
std::apply(
[&](auto... offset) { (new (storage.data() + offset) decltype(fields)::Type(), ...); },
info.offsets
);
}

MetaAggregate(const MetaAggregate& other)
requires(std::copy_constructible<typename decltype(fields)::Type> && ...)
: MetaAggregate(other.refs())
{
}

MetaAggregate(MetaAggregate&& other)
requires(std::move_constructible<typename
godbolt.org Compiler Explorer - C++ namespace detail { // See `type` template <typename T> struct FieldType { using Type = T; }; // Helper for using a string as a template parameter template <std::size_t size> struct ConstexprStringHelper { std::array<char, size - 1> array; constexpr…
Post #25149 7
Poor man's defineaggregate

TLDR: [Try it on Compiler Explorer](
https://godbolt.org/z/sfczoP7hr).

----

While waiting for Clang to support `define
aggregate, I got curious about whether it's possible to do something similar in C++23. Turns out it *kinda* is.

### Rules:
- Only C++23 features;
- No external programs;
- No macros;
- Generated code should be similar to just using a
struct.

----

We start with some helper types:

#include <algorithm>
#include <array>
#include <concepts>
#include <functional>
#include <print>
#include <ranges>
#include <string_view>
#include <tuple>
#include <type_traits>

namespace detail
{

// See
type
template <typename T>
struct FieldType
{
using Type = T;
};

// Helper for using a string as a template parameter
template <std::size_t size>
struct ConstexprStringHelper
{
std::array<char, size - 1> array;

constexpr ConstexprStringHelper(const char (&c_array)[size])
{
std::copy_n(c_array, size - 1, std::begin(array));
}
};

// See
operator""field`
template <auto name>
struct FieldByName
{
};

We calculate the layout of our fake aggregate (sizes, alignment, offsets, ...) at compile time, like so:

// Layout information
template <auto... fields>
struct MetaAggregateInfo
{
static consteval auto calc
align(std::sizet offset, std::sizet align)
{
return (offset + align - 1) & ~(align - 1);
}

static constexpr std::array names{ std::stringview(fields.name)... };
static constexpr std::array sizes{ sizeof(typename decltype(fields)::Type)... };
static constexpr std::array aligns{ alignof(typename decltype(fields)::Type)... };
static constexpr auto max
align = std::ranges::max(aligns);
static constexpr auto offsets = {
std::removeconstt<decltype(sizes)> offsets;
std::sizet nextoffset = 0;

for (auto size, align, offset : std::views::zip(sizes, aligns, offsets))
{
offset = calcalign(nextoffset, align);
nextoffset = offset + size;
}

return offsets;
}();
static constexpr auto total
size = calcalign(offsets.back() + sizes.back(), maxalign);
};

I found it simpler to just use a partial specialization for the case where the aggregate has no members:

template <>
struct MetaAggregateInfo<>
{
static constexpr std::array<std::stringview, 0> names{};
static constexpr std::array<std::size
t, 0> sizes{};
static constexpr std::array<std::sizet, 0> aligns{};
static constexpr auto max
align = 1uz;
static constexpr std::array<std::sizet, 0> offsets{};
static constexpr auto total
size = 1uz;
};

}

A few more helpers:

// Use to declare the type of a field. See example below.
template <typename T>
constexpr detail::FieldType<T> type;

// Type and name of a field
template <typename TheType, std::sizet size>
struct Field
{
using Type = TheType;

detail::FieldType<TheType> type;
std::array<char, size> name;
};

// Use to declare the name of a field
template <detail::ConstexprStringHelper helper>
consteval auto operator""
name()
{
return helper.array;
}

// Use with operator to access a field by name
template <detail::ConstexprStringHelper helper>
consteval auto operator""field() -> detail::FieldByName<helper.array>
{
return {};
}

And now the meat of the code:

template <auto... fields>
class MetaAggregate
{
public:
static constexpr detail::MetaAggregateInfo<fields...> info{};

We define our constructors, copy/move operators and destructor. We use the offsets to get a pointer on which we can do a placement `new`. Other than that, this part is not very interesting.

MetaAggregate()
requires(std::default
initializable<typename decltype(fields)::Type> && ...)
{
std::apply(
& { (new (storage.data() + offset) decltype(fields)::Type(), ...); },
info.offsets
);
}

MetaAggregate(const MetaAggregate& other)
requires(std::copyconstructible<typename decltype(fields)::Type> && ...)
: MetaAggregate(other.refs())
{
}

MetaAggregate(MetaAggregate&& other)
requires(std::move
constructible<typename
godbolt.org Compiler Explorer - C++ namespace detail { // See `type` template <typename T> struct FieldType { using Type = T; }; // Helper for using a string as a template parameter template <std::size_t size> struct ConstexprStringHelper { std::array<char, size - 1> array; constexpr…
Post #25148 12
Great to be back to C++!!!

I went to Java then Scala... then realized my code's performance sucked and C++ 17 was awesome and the portability issue is now moot.

Plus NASA's guidelines for coding where you allocate all you need upfront and never again suits me fine to not need that time consuming performance killing garbage collector.

Just to say glad to be back!

https://redd.it/1t6rmk7
@r_cpp
Reddit From the cpp community on Reddit Explore this post and more from the cpp community
Post #25145 17
C++ Show and Tell - May 2026

Use this thread to share anything you've written in C++. This includes:

* a tool you've written
* a game you've been working on
* your first non-trivial C++ program

The rules of this thread are very straight forward:

* The project must involve C++ in some way.
* It must be something you (alone or with others) have done.
* Please share a link, if applicable.
* Please post images, if applicable.

If you're working on a C++ library, you can also share new releases or major updates in a dedicated post as before. The line we're drawing is between "written in C++" and "useful for C++ programmers specifically". If you're writing a C++ library or tool for C++ developers, that's something C++ programmers can use and is on-topic for a main submission. It's different if you're just using C++ to implement a generic program that isn't specifically about C++: you're free to share it here, but it wouldn't quite fit as a standalone post.

Last month's thread: https://www.reddit.com/r/cpp/comments/1salqls/c_show_and_tell_april_2026/

https://redd.it/1t6eg13
@r_cpp
Reddit From the cpp community on Reddit Explore this post and more from the cpp community
Post #25144 13
noexcept, R (P::*)(Args...) volatile>,
std::conditional_t<is_noexcept, R (P::*)(Args...) noexcept, R (P::*)(Args...)>>>;
};
template<bool is_const, bool is_volatile, bool is_noexcept, typename R, typename P, typename...Args>
using assemble_ptr_to_member_t = assemble_ptr_to_member<is_const, is_volatile, is_noexcept, R, P, Args...>::ptr;

consteval std::meta::info to_ptr_manual(std::meta::info thing) {
bool is_noexcept = std::meta::is_noexcept(thing);
bool is_const = std::meta::is_const(thing);
bool is_volatile = std::meta::is_const(thing);
auto return_t = std::meta::return_type_of(thing);
auto parameters = parameters_of(type_of(thing));
auto parent = type_of(parent_of(thing));
std::vector<std::meta::info> template_args;
if(is_const) {
template_args.push_back(std::meta::reflect_constant(true));
} else {
template_args.push_back(std::meta::reflect_constant(false));
}
if(is_volatile) {
template_args.push_back(std::meta::reflect_constant(true));
} else {
template_args.push_back(std::meta::reflect_constant(false));
}
if(is_volatile) {
template_args.push_back(std::meta::reflect_constant(true));
} else {
template_args.push_back(std::meta::reflect_constant(false));
}
template_args.push_back(return_t);
template_args.push_back(parent);
template_args.append_range(parameters);
return substitute(^^assemble_ptr_to_member_t, template_args);
}

 

For the curious: https://godbolt.org/z/4Gs3n4qa6

The standard could help here if either `add_pointer` got extended, or a new metafunction got invented.

https://redd.it/1t66mbc
@r_cpp
godbolt.org Compiler Explorer - C++ (x86-64 gcc 16.1) struct showcase { void mem_fun() const {}; void looks_like_a_free_function() {}; }; constexpr std::meta::info mem_fun_refl = ^^showcase::mem_fun; constexpr std::meta::info mem_fun_ptr_refl = add_pointer(type_of(mem_fun_refl)); static_assert(mem_fun_ptr_refl…
Post #25143 11
C++ reflections: Getting a reflection of a type of a pointer to member, from a reflection of a member is difficult

I am going to argue that we're missing a fairly basic metafunction in C++26. While there are ways around it, none is without downsides. Let's explore!

The title is a mouthful, but I'm talking about this:

struct showcase {
void mem_fun() const {};
};

constexpr std::meta::info mem_fun_refl = ^^showcase::mem_fun;
constexpr std::meta::info mem_fun_ptr_refl = add_pointer(type_of(mem_fun_refl));

Unfortunately, that last line is just nonsense, because the type of `mem_fun` is `void() const`,
which looks similar to a free function type, with the extra cv qualifier.
`add_pointer`, whether the one in `<meta>` or in `<type_traits>` does not work there and
just produces the same type, unchanged.

 

Things get more confusing if `mem_fun()` does not have a cv qualifier. In that case, its type looks
just like a free function type. Now `add_pointer()` compiles and does the wrong thing.

So `add_pointer()` is not useful at all for this purpose.

 

One option that sometimes works is address-splicing:

constexpr std::meta::info mem_fun_refl = ^^showcase::mem_fun;
constexpr std::meta::info mem_fun_ptr_refl = ^^decltype(&[:mem_fun_refl:]);

That comes with a constraint that `mem_fun_refl` is a constant expression *in the current context*.
In other words, this approach fails when `mem_fun_refl` is an argument to a `consteval` function. I.e. the following does not compile:

consteval std::meta::info to_ptr(std::meta::info thing) {
return ^^decltype([:thing:]);
}

Okay, but we can make `std::meta::info thing` a template parameter. This is what I ended up doing in my project.

template<std::meta::info thing>
consteval std::meta::info to_ptr() {
return ^^decltype([:thing:]);
}

That works, but now whoever calls `to_ptr<thing>()` needs to also have `thing` be a constant expression in that scope. In other words, we end up with propagating "this has to be a template" up the call stack.

 

One last attempt: can we manually assemble a pointer to member's type? Something like

[:return_type:] ([:parent_type:]::*)([:parameter_types:]...)
[:return_type:] ([:parent_type:]::*)([:parameter_types:]...) const
[:return_type:] ([:parent_type:]::*)([:parameter_types:]...) const volatile
[:return_type:] ([:parent_type:]::*)([:parameter_types:]...) volatile
[:return_type:] ([:parent_type:]::*)([:parameter_types:]...) noexcept
[:return_type:] ([:parent_type:]::*)([:parameter_types:]...) const noexcept
[:return_type:] ([:parent_type:]::*)([:parameter_types:]...) const volatile noexcept
[:return_type:] ([:parent_type:]::*)([:parameter_types:]...) volatile noexcept

We have all of the needed info:

bool is_noexcept = std::meta::is_noexcept(thing);
bool is_const = std::meta::is_const(thing);
bool is_volatile = std::meta::is_const(thing);
auto return_t = std::meta::return_type_of(thing);
auto parameters = parameters_of(type_of(thing));
auto parent = type_of(parent_of(thing));

The trouble is now doing the manual assembly without actually splicing anything, because `thing` might not be a constant expression.

This is doable, but is quite involved:

template<bool is_const, bool is_volatile, bool is_noexcept, typename R, typename P, typename...Args>
struct assemble_ptr_to_member {
using ptr = std::condtional_t<is_const,
std::conditional_t<is_volatile,
std::conditional_t<is_noexcept, R (P::*)(Args...) const volatile noexcept, R (P::*)(Args...) const volatile>,
std::conditional_t<is_noexcept, R (P::*)(Args...) const noexcept, R (P::*)(Args...) const>,
std::conditional_t<is_volatile,
std::conditional_t<is_noexcept, R (P::*)(Args...) volatile
Older posts →
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →