Home > front end >  Force decay of string literals (const char array) to ptr
Force decay of string literals (const char array) to ptr

Time:11-27

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)?

> 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()); }'),l:'5',n:'0',o:'C++ source #1',t:'0')),k:47.31070496083551,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:compiler,i:(compiler:g122,deviceViewOpen:'1',filters:(b:'0',binary:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'0',intel:'0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:c++,libs:!(),options:'-Wall -Os --std=c++20',selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:' x86-64 gcc 12.2 (Editor #1)',t:'0')),header:(),l:'4',m:41.3677130044843,n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'x86-64 gcc 12.2',editorid:1,fontScale:14,fontUsePx:'0',j:1,wrap:'1'),l:'5',n:'0',o:'Output of x86-64 gcc 12.2 (Compiler #1)',t:'0')),k:50,l:'4',m:58.632286995515706,n:'0',o:'',s:0,t:'0')),k:52.689295039164485,l:'3',n:'0',o:'',t:'0')),l:'2',n:'0',o:'',t:'0')),version:4" rel="nofollow noreferrer">Demo

#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)

CodePudding user response:

A std::piecewise_construct constructor (as you are trying to use here for the std::pair construction) expects the rest of the arguments to be std::tuples with each tuple holding the arguments to construct one of the pieces (e.g. pair elements).

You are not passing tuples as second and third parameter, so it can't work. It should be

queries.emplace_back(std::piecewise_construct,
                     std::forward_as_tuple("key="),
                     std::forward_as_tuple(params.key_));

However, if you want construction of each pair element from just one argument, you don't need the piecewise constructor. std::pair has a constructor specifically for that:

queries.emplace_back("key=", params.key_);

None of this has anything to do with whether or not the array is decayed to a pointer. (It will be decayed where that is required, no manual intervention required.)

  • Related