abseil-cpp icon indicating copy to clipboard operation
abseil-cpp copied to clipboard

Make StrCat effective for single rvalue string

Open MBkkt opened this issue 2 years ago • 1 comments

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

MBkkt avatar Jul 25 '22 17:07 MBkkt

Can you give a more concrete example?

derekmauro avatar Jul 25 '22 20:07 derekmauro

@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.

MBkkt avatar Oct 06 '22 12:10 MBkkt

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.

derekmauro avatar Oct 06 '22 16:10 derekmauro

This PR breaks users that are relying on the function signature/overload resolution

Ok good point

MBkkt avatar Oct 06 '22 16:10 MBkkt

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.

anpol avatar Oct 06 '22 17:10 anpol

Do you test it? @anpol I already don't remember. But I probably have some problems with overload

MBkkt avatar Oct 06 '22 18:10 MBkkt