方法应返回const std :: string&返回const std :: string_view代替? [英] Should methods returning const std::string& return const std::string_view instead?
问题描述
假设我们在一个类中有一个简单的getter方法,该方法返回对 std :: string
的 const
引用。成员:
Assume we have a simple getter method in a class that returns a const
reference to a std::string
member:
const std::string& getString() const noexcept { return someString; }
随着 std :: string_view
在C ++ 17中,我想知道编写它是否具有任何优势:
With the advent of std::string_view
in C++17, I wonder whether it has any advantages of writing this instead:
const std::string_view getString() const noexcept { return someString; }
一种方法是否比另一种方法有优势/劣势?显然(如果我错了,请纠正我),两种解决方案肯定会比这个更好:
Does one method have advantages/disadvantages over the other? Clearly (correct me if I'm wrong) both solutions will definitely be better than this:
const char* getString() const noexcept { return someString.c_str(); }
我见过与此相关的问题,但我要问的是稍有不同的内容。
I've seen this related question, but I'm asking for something slightly different.
推荐答案
是的,您应该输入:
const std::string& getString() const noexcept { return someString; }
而不是(注意:不是 const
,因为从不返回 const
值):
Instead of (note: not const
, because never return const
values):
std::string_view getString() const noexcept { return someString; }
原因是-您已经有一个字符串
。因此,您不必支付任何额外费用即可获得 string
。并且 string
与任意 string_view
有一个显着的语义差异:它是以空终止的保证。我们知道这一点。也许某些下游用户需要依赖该信息。如果他们需要空终止(例如,他们需要传递给需要它的C API),而您给出了 string_view
,则必须 string
本身。您什么都不做,却有可能使下游用户做更多的工作。
The reason is - you already have a string
. So it's not like you have to pay anything extra to get a string
out of it. And string
has one notable semantic difference to an arbitrary string_view
: it's null-terminated by guarantee. We know this. Maybe some downstream user needs to rely on that information. If they need null-termination (e.g. they need to pass to some C API that requires it) and you give a string_view
, they have to make a string
out of it themselves. You save nothing, but potentially make downstream users do more work.
但是,如果您有 vector< char>
相反...然后,我建议返回 span< char const>
或等效的值。由于没有语义上的差异,您只是提供了一个视图。
If, however, you had a vector<char>
instead... then I would suggest to return a span<char const>
or the equivalent thereof. Since there is no semantic difference and you're just providing a view.
还有一个单独的参数:
There also the separate argument of what:
auto x = obj.getString();
应该这样做。这要么获取 string
字符串的副本(昂贵,但安全),要么有效地引用它(便宜,但可能悬挂)。但是它看起来并不完全像参考,它看起来像是一个价值。一般而言,这是引用语义类型的广泛问题(例如 reference_wrapper
, string_view
, span
, tuple< T& ...>
,可选< T& 如果存在,等等。)
should do. This either takes a copy of the string
(expensive, but safe) or effectively a reference to it (cheap, but potentially dangling). But it doesn't entirely look like a reference, it looks like a value. This is a broad issue with reference-semantic types in general (things like reference_wrapper
, string_view
, span
, tuple<T&...>
, optional<T&>
if it existed, etc.).
对于这种情况,我没有答案,但这是需要注意的。
I don't have an answer for this case, but it's something to be aware of.
这篇关于方法应返回const std :: string&返回const std :: string_view代替?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!