Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

string_view assignment from temporary ?

7 views
Skip to first unread message

Paavo Helde

unread,
Sep 22, 2022, 7:09:25 AM9/22/22
to

I just realized it is legal to assign a temporary std::string to a
std::string_view

std::string foo();

void bar(std::string_view v) {
if (v.empty()) {
v = foo(); // BUG!
}
}


That seems way too dangerous to me. Is there something I could do to get
such things failing to compile? Would deriving from std::string_view and
declaring operator=(std::string&&) as deleted be a solution?


Juha Nieminen

unread,
Sep 22, 2022, 7:17:47 AM9/22/22
to
Paavo Helde <ees...@osa.pri.ee> wrote:
> I just realized it is legal to assign a temporary std::string to a
> std::string_view

I think there may be completely legitimate situations where you may want
to initialize a string_view with a temporary. Like:

foo(std::string_view(bar()));

Paavo Helde

unread,
Sep 22, 2022, 8:19:39 AM9/22/22
to
Yes, I know. That's why my question was about assignment, not
initialization.


Juha Nieminen

unread,
Sep 22, 2022, 8:36:46 AM9/22/22
to
std::string_view view;
foo(view = bar());

Isn't that completely normal code? :P
0 new messages