Live data from Hacker News

Reflection for C++26

isocpp.org

61–70 of 214 posts

Re: Reflection for C++26

#61
post #57
post #34

Earlier quoted context omitted.

C++ has both return type polymorphism and inheritance. What it doesn't have is the kind of type inference that Haskell has. Its type inference is very limited.

You can't have two functions that differ in just the return type in C++

You can sort of do it so long as the return type is a template parameter.

    template
    T my_construct() { T result; return result; }

Re: Reflection for C++26

#62
post #57
post #34

Earlier quoted context omitted.

C++ has both return type polymorphism and inheritance. What it doesn't have is the kind of type inference that Haskell has. Its type inference is very limited.

You can't have two functions that differ in just the return type in C++

You can, if you abuse C++ enough. I needed this very thing for a custom DSL.

    struct PolyReturn {
      const int value;
      operator int() const { return value; }
      operator bool() const { return value > 0; }
    };

https://cppinsights.io/s/f0b9976f

Re: Reflection for C++26

#63

Can I ask a naive question that consists of two parts and please don't flame me? lol * What type of problems static reflection could solve, in general? * Are there specific cases and / or situations where static reflection could resolve such case, even simplify an unnecessary complexity?

What I commonly need is JSON serialization/parsing directly with structs.

They exist, for instance https://github.com/beached/daw_json_link , uses mappings of JSON class members/construction. It handles a bunch of types out of the box and allows custom types to be mapped. Also, it integrates with some of the reflection like libraries out there now(Boost.Describe/Boost.PFR) and will be trivial to add C++26 static reflection when available.

Because the library doesn’t need to allocate unless the underlying types being parsed to do, it has been constexpr since C++17 too.

A bunch of people have used libraries that use macros for reflection like or PFR to integrate other C++ JSON Libraries too.

Re: Reflection for C++26

#64

Earlier quoted context omitted.

This is the thing that's driving me away from C++ very quickly. A big part of our code base is code that handles this, and it either has to be in a DSL and constantly recompiled or we have to make a bunch of boilerplate. It's a huge problem for the language not to be able to do this.

I suggest you to take a look at Boost.Describe. I have a scripting layer in a game that needs to set properties in a C++ Model. I used a single Boost.Describe macro per struct and a generic get/set property. It worked very well and made me get rid of a lot of boilerplate. https://www.boost.org/doc/libs/develop/libs/describe/doc/htm...

Boost.PFR is really neat and doesn’t need manual mapping for aggregate types

Re: Reflection for C++26

#65

Can I ask a naive question that consists of two parts and please don't flame me? lol * What type of problems static reflection could solve, in general? * Are there specific cases and / or situations where static reflection could resolve such case, even simplify an unnecessary complexity?

> What type of problems static reflection could solve, in general? Imagine making a plain struct Point { float x; float y; }; and wanting to serialize it to JSON without further ceremony

This is doable in c++20 https://github.com/stephenberry/glaze

Re: Reflection for C++26

#66
While I love this paper and this proposal in general, as a C++ developer every time C++ adds a new major feature I get somewhat worried about two things:

1. how immense the language has become, and how hard it got to learn and implement

2. how "modernising" C++ gives developers less incentives to convince management to switch to safer languages

While I like C++ and how crazy powerful it is, I also must admit decades of using it that teaching it to new developers has become immensely hard in the last few years, and the "easier" inevitably ends up being the unsafe one (what else can you do when the language itself tells you to refrain from using `new`?).

Re: Reflection for C++26

#67

I haven't touched C++ since undergrad. Neither have I written any Qt code. But from memory, doesn't Qt's moc implement some of this stuff because it wasn't available in C++? Could this replace moc?

I've always wondered what's the point of "replacing moc". I mean what's the problem with moc? It's required by Qt, and completely transparent by the build system. You don't even know it's used.

I mean, GCC also has some helper tools used to compile C++ code and we don't talk about "replacing them".

Why people want to remove moc from Qt?

Re: Reflection for C++26

#68

While I love this paper and this proposal in general, as a C++ developer every time C++ adds a new major feature I get somewhat worried about two things: 1. how immense the language has become, and how hard it got to learn and implement 2. how "modernising" C++ gives developers less incentives to convince management to switch to safer languages While I like C++ and how crazy powerful it is, I also must admit decades…

Yeah that's because you're not supposed to be using "new" anymore since the introduction of smart pointers in C++11. Std::shared_ptr and std::unique_ptr are preferred. Shared pointers ref count and auto-delete, and unique pointers can't be copied.
Post reply on HN