With std::string_view , range::for_each gives an exact assembly with const char[N] and const char * going to std::string_view ctor
In other words, this code
auto str = "the quick brown fox is jumping on a lazy dog\nthe quick brown fox is jumping on a lazy dog\n"; ranges::for_each(std::string_view{str}, std::putchar);
and
auto& str = "the quick brown fox is jumping on a lazy dog\nthe quick brown fox is jumping on a lazy dog\n"; ranges::for_each(std::string_view{str}, std::putchar);
both outputs are below the assembly:
main:
Also, if we pass the string c as const char[N] to ranges::view::c_str() ,
auto& str = "the quick brown fox is jumping on a lazy dog\nthe quick brown fox is jumping on a lazy dog\n"; ranges::for_each(ranges::view::c_str(str), std::putchar);
this gives the exact assembly above, as with one std::string_view .
On the other hand, if we pass the string c as const char* to ranges::view::c_str()
auto str = "the quick brown fox is jumping on a lazy dog\nthe quick brown fox is jumping on a lazy dog\n"; ranges::for_each(ranges::view::c_str(str), std::putchar);
This time it gives another assembly, as shown below:
main:
Which build wins?
Why std::string_view decide to provide the same binary?
Can view::c_str() get only one faster build with const char* and const char [N] ?
godbolt.org/g/wcQyY1