简体   繁体   English

应该返回const std :: string并返回const std :: string_view吗?

[英]Should methods returning const std::string& return const std::string_view instead?

Assume we have a simple getter method in a class that returns a const reference to a std::string member: 假设我们在类中有一个简单的getter方法,它返回对std::string成员的const引用:

const std::string& getString() const noexcept { return someString; }

With the advent of std::string_view in C++17, I wonder whether it has any advantages of writing this instead: 随着C ++ 17中std::string_view的出现,我想知道它是否具有写这个的任何优点:

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. 我已经看到了这个相关的问题,但我要求的是略有不同的东西。

Yes, you should write: 是的,你应该写:

const std::string& getString() const noexcept { return someString; }

Instead of (note: not const , because never return const values): 而不是(注意:不是const ,因为永远不会返回const值):

std::string_view getString() const noexcept { return someString; }

The reason is - you already have a string . 原因是 - 你已经有了一个string So it's not like you have to pay anything extra to get a string out of it. 所以这并不是说你需要支付任何额外费用来获取它的string And string has one notable semantic difference to an arbitrary string_view : it's null-terminated by guarantee. string与任意string_view有一个显着的语义差异:它由保证以null结尾 We know this. 我们知道这一点。 Maybe some downstream user needs to rely on that information. 也许某些下游用户需要依赖该信息。 If they need null-termination (eg 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. 如果他们需要 null终止(例如他们需要传递给需要它的某个C API)并且你给了一个string_view ,他们必须自己创建一个string You save nothing, but potentially make downstream users do more work. 您什么都不保存,但可能会让下游用户做更多工作。

If, however, you had a vector<char> instead... then I would suggest to return a span<char const> or the equivalent thereof. 但是,如果你有一个vector<char>而不是......那么我建议返回一个span<char const>或其等价物。 Since there is no semantic difference and you're just providing a view. 由于没有语义差异,您只是提供一个视图。


There also the separate argument of what: 还有一个单独的论点:

auto x = obj.getString();

should do. 应该做。 This either takes a copy of the string (expensive, but safe) or effectively a reference to it (cheap, but potentially dangling). 这需要一个string的副本(昂贵但安全)或有效地引用它(便宜,但可能悬空)。 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.). 这是一般参考语义类型的广泛问题(如reference_wrapperstring_viewspantuple<T&...> ,如果存在,则optional<T&>等)。

I don't have an answer for this case, but it's something to be aware of. 我对这个案子没有答案,但需要注意的是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM