abseil / abseil-cpp

Abseil Common Libraries (C++)
https://abseil.io
Apache License 2.0
14.7k stars 2.57k forks source link

[Bug]: `ABSL_FLAG(std::string)` does not accept `std::string_view` as a default value #1540

Open jiawen opened 11 months ago

jiawen commented 11 months ago

Describe the issue

When defining a flag of std::string type, the default value must be an std::string or const char*. It would be great if std::string_view were also accepted.

Following Google conventions, I frequently have string constants in the form:

constexpr std::string_view kMyStringConstant = "asdf";

which I tend to prefer over constexpr const char* because std::string_view has a constexpr constructor that allows explicit zeroes in strings.

Steps to reproduce the problem

The following does not compile:

constexpr std::string_view kMyStringConstant = "asdf";

ABSL_FLAG(std::string, my_flag_name, kMyStringConstant, "my message");

Of course, I can work around this with:

ABSL_FLAG(std::string, my_flag_name, std::string(kMyStringConstant), "my message");

What version of Abseil are you using?

Pinned to commit 74eee2aff683cc7dcd2dbaa69b2c654596d8024e

What operating system and version are you using?

MacOS Ventura 13.5.2

What compiler and version are you using?

$ clang -v
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: arm64-apple-darwin22.6.0
Thread model: posix
InstalledDir: /Applications/Xcode_14.3.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

What build system are you using?

bazel 6.1.1

Additional context

No response

derekmauro commented 11 months ago

If someone comes up with a patch to this that doesn't break existing code, then I will consider accepting it, but as you said, the workaround is so trivial that I don't think anyone is going to spend time looking into this.