In the following example I try to emplace_back()
a string literal and a string_view
into an std::pair
of string_views
. However, my problem is that emplace_back()
takes up the string literal as a const char array and doesn't decay it to a pointer. The consequence is that emplace_back()
can't find std::string_view
's const char* constructor. How can I decay string literals manually as parts of function arguments (that are aggressively gobbled up by universal references)?
#include <utility>
#include <vector>
#include <string>
#include <string_view>
#include <cstdio>
struct A
{
std::string_view vsn_ = "0.0.1";
std::string_view key_;
};
auto get_url(A params = {})
{
std::vector<std::pair<const char*, std::string_view>> queries;
if (!params.key_.empty()) {
queries.emplace_back(std::piecewise_construct, ("key="), params.key_);
}
if (!params.key_.empty()) {
queries.emplace_back(std::piecewise_construct, ("VSN="), params.vsn_);
}
std::string url;
for (auto q : queries) {
url += q.first;
url += q.second.data();
}
return url;
}
int main()
{
printf("%s", get_url().data());
}
(As you can see I already tried to put parantheses around the string literals to no avail)