Consider the following case: the second static_assert
fails (https://gcc.godbolt.org/z/KP4zxvY7G).
#include <cstdio>
#include <type_traits>
#include <vector>
std::vector<int> generate_numbers();
int main()
{
// Ok
{
std::vector<int> f = generate_numbers();
auto lambda = [=] { printf("%d", (int)f.size()); };
static_assert(std::is_nothrow_move_constructible_v<decltype(lambda)>);
}
// Not good
{
const std::vector<int>& f = generate_numbers();
auto lambda = [=] { printf("%d", (int)f.size()); };
static_assert(std::is_nothrow_move_constructible_v<decltype(lambda)>);
}
}
I would expect both static_assert to pass as making a copy of a const value in my mind creates a new value which does not need to preserve the constness.
But this is not what seems to happen, which is actually problematic in generic code where one may want to rely on lifetime-extension to prevent unneeded copies:
void f(auto&& foo) {
const auto& result = foo();
...
}
What can be done here ? I can use C++20.