abseil-cpp
abseil-cpp copied to clipboard
Make StrCat effective for single rvalue string
Main motivation for me is patter like:
void SomeCall(Args&&...);
auto msg = StrCat(...);
LOG(unique_id, msg);
SomeCall(std::move(msg)); // internally have absl::StrCat for case when argument more than one or some of them not is string
Can you give a more concrete example?
@derekmauro
Can you give a more concrete example?
I write function that looks like log(some sink, parameters);
, this function concat parameters to single string.
Sometime parameters are single parameter.
And sometime it is rvalue string
I don't understand why do you want a make copy in this case.
This patch fix it.
This PR breaks users that are relying on the function signature/overload resolution. I ran it through our code base verify this and found lots of code that no longer builds.
I asked for a complete example so that I might be able suggest an alternative. If you want to provide a complete example maybe I can help.
This PR breaks users that are relying on the function signature/overload resolution
Ok good point
Excuse me, may I come up with an example?
#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include "absl/log/log.h"
template<typename... Args>
void log_info(Args&&... args) // A library function.
{
LOG(INFO) << absl::StrCat(std::forward<Args>(args)...);
}
void usage_example(std::vector<std::string> vec)
{
log_info(absl::StrJoin(vec, ", "));
}
I would suggest adding an overload to handle this, e.g.:
ABSL_MUST_USE_RESULT inline std::string StrCat(std::string&& arg) {
return std::move(arg);
}
That doesn't seem to break anything.
BTW, original patch uses if constexpr
, which is a C++17 feature, while Abseil is C++14.
Do you test it? @anpol I already don't remember. But I probably have some problems with overload