C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Proposal: Disallow string(_view) construction from two literals

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Wed, 28 Oct 2026 16:29:17 +0300
On 28 Oct 2026 15:19, Johannes Kalmbach via Std-Proposals wrote:
> Hi,
> The following snippet compiles, but invokes UB, because overload
> resolution treats the decayed pointers to the two string literals as
> begin an end of the string:
>
> `std::string s{"undefined", "behavior"}`
>
> I would propose disallowing this antipattern, similar to P2166 disallows
> string construction from `nullptr`.

I feel like this, and the deleted constructor from nullptr, are pretty
much useless in practice. The nullptr one disallows construction when
the user explicitly specifies nullptr as the argument. Noone does that
in practice. The only reasonable case where that could happen is when
the argument is forwarded by the caller from an unconstrained template
argument, but then you have to wonder if the correct fix would be to
constrain the caller. The deleted constructor does not prevent a null
pointer to character type from being passed, which is the real cause of
bugs of this kind most of the time.

Same with the proposed two-literals constructor - noone writes that
code, and again it doesn't help with real cases, when the caller
accidentally passes two unrelated pointers to characters.

And while, yes, the nullptr one got accepted, I do not think this alone
is a good enough motivation to add more similar deleted constructors.

PS: No, I'm not proposing to add runtime checks for null or that input
iterators are reachable to prevent user mistakes.

Received on 2026-10-28 13:29:25