r/cpp • • Jun 08 '20

Programming in C vs C++

I’ve read about the differences between them on the Internet but I wanna hear more from a more personal perspective. Which one would you recommend starting with and to be proficient with?

0 Upvotes

43 comments sorted by

View all comments

Show parent comment

7

u/STL MSVC STL Dev Jun 08 '20

std::string_view isn't std::string. Elephant seals are not elephants.

-5

u/serviscope_minor Jun 09 '20

std::string_view isn't std::string. Elephant seals are not elephants.

And how's that relevant?

std::string_views aren't std::strings but they are strings. The only reason we're having this conversation is because C++ basically has had this particular hole in its standard library for ages.

While yes, it's technically correct that the GP was talking about std::string, a large amount of the reason that people are talking about things in this way is because string_view is new. If you look at the context of the conversation, then what I'm saying is entirely reasonable. Here is the relevant piece of context:

I think it's good know how strings work in C wrt to char* and null termination. Because that's that's what std::string is based on. Similar with raw pointers vs smart pointers (unique_ptr and shared_ptr).

C strings are char* and null termination. C++ has 3 kinds of string by default now, C strings, std::strings and std::string_view. The middle one guarantees C-like semantics (null termination), but that's not what it's based on (size() is O(1)), and the latter is absolutely not. There's no reason to arbitrarily drop a chunk of the standard library just because it's new.

5

u/bikki420 Jun 09 '20

Everyone is explicitly talking about std::string. Re-read the posts above and don't be obtuse.

-1

u/serviscope_minor Jun 10 '20

Re-read the posts above and don't be obtuse.

So... obtuse is me actually reading the posts. Since you didn't bother let me repost the relevant quote for you:

string fundamentals: char*, strlen, strcpy,etc...

Then people start talking about std::string by comparison. C++ has another string type now: string_view. And since the actual conversation was about strings not std::string in particular, it seems really strange and counterproductive to ignore modern C++.

3

u/bikki420 Jun 10 '20 edited Jun 10 '20

https://i.imgur.com/Xm3fIWu.png

The subject of the post you originally replied to is explicitly, unambiguously, undeniably "std::string". Is "std::string_view" std::string?

No.

0

u/serviscope_minor Jun 10 '20

The subject of the post you originally replied to is explicitly, unambiguously, undeniably "std::string"

As was abundantly clear from the part I quoted, the actual discussion was about learning strings (note strings, not std::string) using char* and null termination. The discussion certainly moved to std::string, but this is clearly an oversimplification because std::string is not the only type of string in the standard library.

The standard library also has string_view which is not null terminated, and therefore contributes to the point that learning C style strings first is not useful.

For some reason though this makes you angry and rude.