'runtime/string' cpplint; returning a const std::string&

492 views
Skip to first unread message

Nigel Tao

unread,
Nov 14, 2018, 1:07:37 AM11/14/18
to chromi...@chromium.org
I have a static string constant:

class Foo {
// Etc.

// An empty string that some methods return a reference to.
static const std::string empty_string_;
}

https://chromium-review.googlesource.com/c/chromium/src/+/1335071/1/chrome/services/app_service/public/cpp/app_update.h#59

that I use because a method wants to return a "const std::string&":

const std::string& AppUpdate::Name() const {
// Etc.
return empty_string_;
}

https://chromium-review.googlesource.com/c/chromium/src/+/1335071/1/chrome/services/app_service/public/cpp/app_update.cc#66

cpplint complains with "For a static/global string constant, use a C
style string instead: "const char AppUpdate::empty_string_[]".
[runtime/string] [4]"

but a C style string doesn't work here. I don't want a particular
sequence of characters. I want a fallback *value* (specifically of
type std::string) that I can return a const reference to, that the
caller can use *after the function returns*, but the caller also
shouldn't destroy that std::string.

What should I do instead, to please cpplint?

Lei Zhang

unread,
Nov 14, 2018, 1:15:41 AM11/14/18
to nige...@chromium.org, chromi...@chromium.org
return base::EmptyString() instead?
> --
> --
> Chromium Developers mailing list: chromi...@chromium.org
> View archives, change email options, or unsubscribe:
> http://groups.google.com/a/chromium.org/group/chromium-dev
> ---
> You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
> To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CAEdON6ZirDoNFPCbkkxwjBy4y_7Kb18Do_Kc3vx2MGA9H4ZKHw%40mail.gmail.com.
>

Sam McNally

unread,
Nov 14, 2018, 1:15:47 AM11/14/18
to nige...@chromium.org, chromi...@chromium.org

Nigel Tao

unread,
Nov 14, 2018, 4:35:31 PM11/14/18
to chromi...@chromium.org
base::EmptyString is perfect. Thanks.
Reply all
Reply to author
Forward
0 new messages