C++ Weekly - Ep 262 - std::string's 11 Confusing Constructors

preview_player
Показать описание
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟

T-SHIRTS AVAILABLE!

WANT MORE JASON?

SUPPORT THE CHANNEL

GET INVOLVED

JASON'S BOOKS

► C++23 Best Practices

► C++ Best Practices

JASON'S PUZZLE BOOKS

► Object Lifetime Puzzlers Book 1

► Object Lifetime Puzzlers Book 2

► Object Lifetime Puzzlers Book 3

► Copy and Reference Puzzlers Book 1

► Copy and Reference Puzzlers Book 2

► Copy and Reference Puzzlers Book 3

► OpCode Puzzlers Book 1


RECOMMENDED BOOKS

AWESOME PROJECTS

O'Reilly VIDEOS

Рекомендации по теме
Комментарии
Автор

Funny thing is that this kind of baggage will probably stay in C++ till the end, no matter how ultra quantum postmodern it becomes

dennisgrishin
Автор

The overloaded constructor, replacing the defaulted count, is in cppreference in the video right below the constructor that is highlighted

mogenshansen
Автор

Cppreference is correct, you just misread it a bit. They already removed the defaulted size parameter in C++17. C++20 just added constexpr.

This just made me think of a great idea for cppreference, although it might be a bit hard to implement in retrospective: The ability to only show information about the version you're interested in, without all the clutter of changes in older/newer versions. Don't you have some connections to the guys at cppreference, Jason? ;)

Possseidon
Автор

Well it makes sense to expect a character pointer to point at the first position you want to use.

hex
Автор

Pretty easy to explain: the CharT* constructor can receive an offseted string (just add an arbitrary number to the pointer). While you cannot easily (i.e. w/o unnecesary overhead) offset an std::string, except for passing the offset as a paramter.

KennyMinigun
Автор

Ok committee, I am ready for incompatible but consistent next c++ standard

serhiymalokhatko
Автор

The logic behind is that for a char pointer you don't need a position, you can just add that to the pointer and pass the resulting pointer as the first argument. std::string x(ptr + pos, len);

cppsage
Автор

Whenever I am using that " 3, 'a' " constructor, I just constantly remind myself: this is just like a vector of char.

rinkaenbyou
Автор

I have never used that overload. Deprecate for C++23 or C++26. Remove in C++29.

milasudril
Автор

"In C++20 this became constexpr" -- We just don't know how.

technologicalwaste
Автор

I wish there’d be compiler switches for keeping that kind of bad stuff around, as in, remove it from the standard but provide “--includePre20Junk” to allow keeping compatibility, or something like that.

Runoratsu
Автор

I want a C++ version of what Rust does with its "Editions". Which would solve problems like this specifically, I believe.

simonfarre
Автор

If I could design C++ std::string I'd add some named constructors and remove some confusing unnamed ones.
And that solves all issues as far as I can see.
This can be done since C++17 with standardized RVO/NRVO.

danielmilyutin
Автор

An idea for a C++ best practice or guideline: constructors for container-like types (maybe others too) should only handle getting the data into the constructed object, with no transformation and as little logic as possible. Anything else should be NAMED factory functions.
The number of times I've run afoul of the "repeated value" constructor for std::vector when I was just trying to pass two ints is actually ridiculous, and no, braced initialization isn't always the answer. This only gets worse when you're dealing with templates and forwarding constructors.

Dth
Автор

A simple fix would be to avoid C string when possible, since we have constexpr string_view for literal.

howareyou
Автор

Everything I do in C++ seems to have confusing constructors.

IllumTheMessage
Автор

NaN => Not a Number. npos => not a position.

cppsage
Автор

I dunno. The (string, pos, count) pattern is consistent with all the other string member functions. I’ve never found this confusing in practice. Char pointers and strings are truly very different types and even in places where they can be used interchangeably, you really need to pay attention to which one you’re actually using. The fact that string{3, ‘a’} gives you different results than string(3, ‘a’) bothers me more...even though I understand why.

RobertFisher
Автор

What would be the fun in programming if there was never any confusion?

bettkitty
Автор

This is why we need named constructors lol

alextrotta