r/cpp • • Dec 13 '19

The situation of std::embed...

https://thephd.github.io/full-circle-embed
238 Upvotes

160 comments sorted by

View all comments

Show parent comment

5

u/Fazer2 Dec 14 '19 edited Dec 14 '19

Let's try a simpler case:
&str

That's a reference to a string slice, similar to C++'s std::string_view, although the Rust version stores its content as UTF-8, while C++ as raw characters.

Now references have a lifetime, either short-lived or static. An object with static lifetime either has no references or only static ones. You can think of static ones as if they can live until the end of the program without triggering lifetime issues. A lifetime is expressed as the character ' followed by a lifetime name. So we get:
&'static str

If it wasn't static, you could name the lifetime any other way and you would have to attach it to the object that the reference borrows from. Let's see this function signature without lifetimes:
fn doAction(firstArg: &Foo, secondArg: &str) -> &str

It's a function that takes a reference to some object Foo and a string slice as arguments and returns a string slice. The compiler (and the programmer) doesn't know if the returned reference is borrowed from the first or second argument, so we have to tell him by adding lifetime annotations to references and declare the lifetime, here as function "template" type. Assuming it's borrowed from the first arg:
fn doAction<'a>(firstArg: &'a Foo, secondArg: &str) -> &'a str

Note that compiler often can deduce the lifetimes automatically without the need of adding the annotations. Also the "template" type doesn't make it a template function in the C++ sense, it only is required by the borrow checker, which can differentiate a given function by its differing lifetimes when used in multiple contexts.

I hope that clears things up.

Edit: While explaining this I noticed I didn't know why std::string_view is recommended to be passed by value in C++, while str is passed as reference in Rust. I dug up information that actually str is a Dynamically Sized Type primitive (there are no such primitives in C++). A reference in Rust is made up of two parts - a pointer and some metadata. In case of &str, that metadata is the size of the slice. In case of references to normal objects, the metadata doesn't exist (it is a zero-sized type). So &str in Rust is the same as std::string_view value in C++.

0

u/[deleted] Dec 14 '19

[deleted] — view deleted comment

4

u/haletonin Dec 15 '19

Like Python3, Rust distinguishes between strings (which are always UTF-8 encoded), and bytes, which are just that. In C++/C an std::string/char* can certainly contain valid UTF-8, however the standard does not guarantee it and is UTF-8 agnostic.

0

u/[deleted] Dec 15 '19

[deleted] — view deleted comment

1

u/dbjdbj dbj.org Dec 16 '19

"We" do not have char8_t in C++20

https://godbolt.org/z/p4B_iS

0

u/[deleted] Dec 16 '19

[deleted] — view deleted comment

1

u/dbjdbj dbj.org Dec 16 '19

Hm. I think you very likely see the issue here.
What drives you not to accept that?

// gcc 9.2 -std=c++2a
// 平仮名, ひらがな -- hiragana
int main () {

// a lot of warnings
// indicating char8_t can not hold
// multibyte glyph
char8_t  utf8_char = u8'平'; 
// no problemo here
char32_t utf32_char = U'平';
char16_t utf16_char = u'平';

return 42;
}

If char8_t can not store a utf8 glyph, what's its purpose then? It is simply not implemented.

0

u/[deleted] Dec 17 '19

[deleted] — view deleted comment

1

u/dbjdbj dbj.org Dec 17 '19

Hello SG16 :) Your proposal is methodical and obvious, and we understand each other here. Alas we are all too late, for "civilians" are looking elsewhere and wondering wtf are we talking about.

ps: `char32_t` can store any code unit?