-
Notifications
You must be signed in to change notification settings - Fork 70
Open
Description
Hi, I am not convinced by the code section about NRVO:
std::optional<std::string> make_heavy_object_mutable() {
std::string x(1024, 'x');
return x;
}
std::optional<std::string> make_heavy_object_immutable() {
std::string const x(1024, 'x'); //! `const` is the only difference
return x;
}
static void rvo_friendly(bm::State &state) {
for (auto _ : state) bm::DoNotOptimize(make_heavy_object_mutable());
}
static void rvo_impossible(bm::State &state) {
for (auto _ : state) bm::DoNotOptimize(make_heavy_object_immutable());
}
It states that the const
prevents NRVO, but cv qualifications actually don't inhibit it (https://timsong-cpp.github.io/cppwp/n4659/class.copy#elision-1.1). I think this test isn't checking RVO at all, as the return type is actually std::optional
, not std::string
. What the const
inhibits is moving the string inside the optional, and instead it forces the use of the copy constructor (that has to perform a memcpy).
Metadata
Metadata
Assignees
Labels
No labels