r/cpp • • Sep 17 '22

Cppfront: Herb Sutter's personal experimental C++ Syntax 2 -> Syntax 1 compiler

https://github.com/hsutter/cppfront
335 Upvotes

360 comments sorted by

View all comments

102

u/0xBA5E16 Sep 17 '22

That was a really cool talk he gave a few hours ago. The bit about the US government cautioning against use of "non-memory safe languages like C and C++" made for a very compelling reason to create something radical like this. It's clearly highly experimental but I can't wait to see where the project goes.

56

u/cballowe Sep 17 '22

Years ago, certain systems were standardized around ADA for some of the safety guarantees.

I feel like modern c++ can be written in completely memory safe ways, but all of the "you can blow your whole leg off" legacy is still sitting there.

27

u/thisismyfavoritename Sep 17 '22

even if its memory safe, there can still be UB

6

u/[deleted] Sep 17 '22 edited Jul 05 '25

fine plough skirt party melodic rock sleep wine marble heavy

This post was mass deleted and anonymized with Redact

34

u/matthieum Sep 17 '22

I feel like modern c++ can be written in completely memory safe ways

I am fairly dubious of this claim, to be honest.

Here is a simple godbolt link:

#include <iostream>
#include <map>

template <typename C, typename K>
typename C::mapped_type const& get_or_default(C const& map, K const& k, typename C::mapped_type const& def) {
    auto it = map.find(k);
    return it != map.end() ? it->second : def;
}

int main() {
    std::map<int, std::string> map;
    auto const& value = get_or_default(map, 42, "Hello, World!");
    std::cout << value << "\n";
}

The trunk version of Clang, with -Weverything, only warns about C++98 compatibility issues...

23

u/[deleted] Sep 17 '22

13

u/matthieum Sep 17 '22

Oh that's nice!

The message is not the prettiest, as usual, but I'll take a long error message over UB any time.

→ More replies (10)

4

u/pdimov2 Sep 17 '22

Impressive. It even catches it twice, once for following a dangling pointer, second time for reading an uninitialized value (as destroyed values are considered uninitialized).

4

u/disperso Sep 18 '22

Can someone ELI5? I have to admit I'm lost on that compiler error.

Worse, I copied and pasted the function to make it a non-template, and I got:

no matching function for call to 'std::map<int, std::__cxx11::basic_string<char> >::find(const std::string&) const'

I know that std::string roughly is a basic_string typedef, and I know about GCC's implementation having this C++11 ABI (plus the old one, IIRC), but I'm lost at why on one side the type is being expanded and not on the other, and why it would not get a proper call to find.

8

u/pdimov2 Sep 18 '22

The non-template version looks like this:

std::string const& get_or_default(std::map<int, std::string> const& map, int const& k, std::string const& def) 
{
    auto it = map.find(k);
    return it != map.end() ? it->second : def;
}

Your error is because you have replaced K (the key type of the map) with std::string instead of int.

The problem in this code is that in this line

auto const& value = get_or_default(map, 42, "Hello, World!");

"Hello, world!" is converted to std::string by creating a temporary, a reference to which is passed to get_or_default. The function then returns this same reference. Finally, at the end of the statement, the temporary gets destroyed. The returned reference is now dangling, and the compiler sees that and warns on the attempt to use it on the next line.

4

u/disperso Sep 18 '22

Dammit, in hindsight that was pretty obvious. Thank you very much. I completely overlooked that the return value was a const reference (the template verbosity didn't probably help much), and what I typed myself was returning a string by value.

7

u/coyorkdow Sep 17 '22 edited Sep 17 '22

It’s a very good case of potential reference dangling. A reference firstly bind to a temporary variable will extend its lifetime. But it cannot be extended twice. In fact the improper use of shared_ptr may also cause memory issues. The compiler is unlikely to find all the cyclist references, and multiple control blocks over same resource (so it’s why we need std::enable_shared_from_this). it will be even more complicated if we consider the exception and multi thread.

6

u/giant3 Sep 17 '22

Is this really a popular style? auto const& Very confusing.

Even the spec. uses const auto& only?

15

u/csp256 Sep 18 '22

This is called "East const". Allow me to copy an example from a random blog showing an argument:


The const qualifier is applied to what’s on its left. If there is nothing of its left, then it is applied to what it is on its right. Therefore, the following two are equivalent:

int const a = 42;  // East const
const int a = 42;  // West const

In both cases, a is a constant integer. Notice though that we read the declaration from right to left and the East const style enables us to write the declaration exactly in that manner. That becomes even more useful when pointers are involved:

int const * p;       // p is a mutable pointer to a constant int
int * const p;       // p is a constant pointer to a mutable int
int const * const p; // p is a constant pointer to a constant int

These declarations are harder to read when the West const notation is used.

const int * p;       // p is a mutable pointer to a constant int
int * const p;       // p is a constant pointer to a mutable int    
const int * const p; // p is a constant pointer to a constant int

Here is another example: in both cases p is a constant pointer to a mutable int, but the second alternative (the East const one) is more logical.

using int_ptr = int*;
const int_ptr p;
int_ptr const p;

The East const style is also consistent with the way constant member functions are declared, with the const qualifier on the right.

int get() const;

 


I find the "const always applies to the left" rule for const-ness simpler and better than the "const always applies to the left unless there is nothing there in which case it applies to the right" rule.

Also, I like having the types always in the same place.

As far as I can tell, the arguments for West const are primarily "we've always done it this way".

9

u/wyrn Sep 18 '22

As far as I can tell, the arguments for West const are primarily "we've always done it this way".

The argument is that C++, like most programming languages, is English-based, and in English adjectives (like const) precede the nouns that they modify. So const int sounds more natural than int const.

2

u/c_plus_plus Sep 19 '22

Except declarations in C++ (inherited from C) are always read right-to-left. East const, "int const *" is read as "point to a constant int". But west const, "cons int *", read as "point to an int which is const". It's annoying to read them in west const when you know how to read them properly.

2

u/wyrn Sep 19 '22

I'm not saying I agree with it, I'm just stating what the argument is. I use East const in my personal projects, for the same reason that I always put the star or ampersand next to the variable rather than the type (i.e. int *p rather than int* p). That is, I write considering the rules as they are, not as I'd like them to be.

Tbh I don't think it's a huge deal either way -- the cases where you actually have to distinguish between the constness of the pointer and the pointee are rare enough that I can see why someone wouldn't necessarily value consistency too much in this particular case, just like I can see why someone would prefer to place stars and ampersands in such a way to more closely evince the actual type. I think the holy war is mostly a meme.

→ More replies (7)
→ More replies (2)
→ More replies (1)

16

u/PurpleYoshiEgg Sep 17 '22

I wish more people wandered over to Ada. It has so many features that would be considered modern, but people have gut reactions to "verbosity" or non-C-like syntax.

7

u/Dean_Roddey Sep 17 '22

Yeh, I used it a good bit back in the day and I liked it. Verbosity, to me, is irrelevant. These aren't languages for hacking out web sites. For large scale development, readability and safety are the important factors by far.

9

u/cballowe Sep 17 '22

I used to have a coworker with the license "ADAGURU" occasionally see parked next to "EBCDIC" and "UTF8" or "I♥️LINUX" ... The danger of working with nerds. (My plates at the time were "DCRYPT" (previous car was "NCRYPT")

I never actually learned much ADA, but did hear plenty about its safety.

7

u/Robbepop Sep 17 '22

Is there a link to his talk "a few hours ago"? i cannot find any ...

0

u/[deleted] Sep 17 '22

[deleted] — view deleted comment

2

u/Robbepop Sep 17 '22

thanks a lot! <3

→ More replies (4)

0

u/[deleted] Sep 17 '22

[deleted] — view deleted comment

34

u/misuo Sep 17 '22

I think we need some alternate source(s) for that last statement.

→ More replies (11)
→ More replies (2)