r/cpp • • May 01 '23

cppfront (cpp2): Spring update

https://herbsutter.com/2023/04/30/cppfront-spring-update/
223 Upvotes

164 comments sorted by

View all comments

41

u/Nicksaurus May 01 '23

First thing, it looks like there's a typo in the description of the struct metaclass:

Requires (else diagnoses a compile-time error) that the user wrote a virtual function or a user-written operator=.

Those things are disallowed, not required (/u/hpsutter)


Anyway, on to the actual subject of the post. Every update I read about cpp2 makes me more optimistic about it. I'm looking forward to the point where it's usable in real projects. All of these things stand out to me as big improvements with no real downsides:

  • Named break and continue

  • Unified syntax for introducing names

  • Order-independent types (Thank god. I wish I never had to write a forward declaration again in my life)

  • Explicit this

  • Explicit operator= by default

  • Reflection!

  • Unified function and block syntax

A few other disorganised thoughts and questions:


Why is the argument to main a std::vector<std::string_view> instead of a std::span<std::string_view>? Surely the point of using a vector is to clearly define who has ownership of the data, but in this case the data can only ever belong to the runtime and user code doesn't need to care about it. Also, doesn't this make it harder to make a conforming implementation for environments that can't allocate memory?


Note that what follows for ... do is exactly a local block, just the parameter item doesn’t write an initializer because it is implicitly initialized by the for loop with each successive value in the range

This part made me wonder if we could just use a named function as the body of the loop instead of a parameterised local block. Sadly it doesn't seem to work (https://godbolt.org/z/bGWPdz7M4) but maybe that would be a useful feature for the future


Add alien_memory<T> as a better spelling for T volatile

The new name seems like an improvement, but I wonder if this is enough. As I understand it, a big problem with volatile is that it's under-specified what exactly constitutes a read or a write. Wouldn't it be better to disallow volatile and replace it with std::atomic or something similar, so you have to explicitly write out every load and store?


Going back to the parameterised local block syntax:

//  'inout' statement scope variable
// declares read-write access to local_int via i
(inout i := local_int) {
    i++;
}

That argument list looks a lot like a lambda capture list to me. I know one of the goals of the language was to remove up front capture lists in anonymous functions, but it seems like this argument list and the capture operator ($) are two ways of expressing basically the same concept but with different syntax based on whether you're writing a local block or a function. I don't have any solution to offer, I just have a vague feeling that some part of this design goes against the spirit of the language

33

u/kreco May 01 '23

Why is the argument to main a std::vector<std::string_view> instead of a std::span<std::string_view>?

I was wondering the same.

3

u/Zeh_Matt No, no, no, no May 02 '23

I mean does it really matter here? You could just continue passing the arguments as a view from here on out. I'm fine with either way as long its no longer argc, argv.

7

u/SkoomaDentist Antimodern C++, Embedded, Audio May 02 '23

I mean does it really matter here

It does. vector requires some form of heap while span can point to const data (and can itself be constructed at compile / link time).

1

u/Zeh_Matt No, no, no, no May 03 '23

You are not wrong about vector using additional memory but you can not construct a span for the command line arguments at compile time, the pointer passed is also heap so the address is not known at compile time. I don't disagree that it should be span but at the same time I'll take vector anytime over the C style entry point.

1

u/SkoomaDentist Antimodern C++, Embedded, Audio May 03 '23 edited May 03 '23

I don't see why span could not be constructed at compile time on the systems where heap usage is actually a problem - namely bare metal embedded. There's nothing in regular main() that says the commandline arguments have to be stored in heap and this is essentially just a wrapper around that. Both span and string_view are just (pointer, length) pairs under the hood, so they should be able to be constructed at compile time as long as the pointer and length are known (ie. all arguments are fixed).

1

u/Zeh_Matt No, no, no, no May 03 '23

How do you know at compile time how many arguments the user passed during runtime? In order to construct a span you need start + length, you may know the start during compile time if you have fixed storage but length will be not known until the user actually supplies any arguments so therefor you can not construct a span at compile time for the command line parameter, this is literally impossible.

1

u/SkoomaDentist Antimodern C++, Embedded, Audio May 03 '23

In bare metal embedded context the arguments are typically baked in at compile time (Your code is the OS).

The problem with using vector there is that the signature of main() then forces normal heap to be used which can be a major issue on some platforms (as opposed to using a custom allocator). All for no particular benefit.

2

u/Zeh_Matt No, no, no, no May 03 '23

How does the compiler know what the user provides as arguments?

1

u/SkoomaDentist Antimodern C++, Embedded, Audio May 03 '23 edited May 03 '23

Because the "user" aka the developer's build environment literally inserts the arguments in a static table (in this context).

Edit: Having the arguments constructed at compile time is a nice benefit but what's the most important is avoiding anything that requires the use of regular heap (ie. the standard std::vector). Building the argument list in a static table at runtime is often an acceptable solution even if not quite as optimal.

→ More replies (0)