vendredi 15 janvier 2021

some weird behavior of std::make_pair with deduction of string_view

std::string api_key_ = "123456789-5c51509f-8c5c5dc2-b6557";
std::pair<std::string_view, std::string_view> yy = std::make_pair("AccessKeyId", api_key_);
std::cout << "yy.second:" << yy.second << std::endl;

compile with c++17 this will output:

yy.second:9-5c51509f-8c5c5dc2-b6557

while this is good without deduction

std::string api_key_ = "123456789-5c51509f-8c5c5dc2-b6557";
std::string_view sv = api_key_;
std::cout << "sv:" << sv << std::endl;

std::pair<std::string_view, std::string_view> xx = std::make_pair<std::string_view, std::string_view>("AccessKeyId", api_key_);
std::cout << "xx.second:" << xx.second << std::endl;

output:

sv:123456789-5c51509f-8c5c5dc2-b6557
xx.second:123456789-5c51509f-8c5c5dc2-b6557

Can anybody explain? Thanks. the complete code is as below:

#include <unordered_map>
#include <iostream>
#include <variant>
#include <deque>
#include <vector>

int main() {
    std::string api_key_ = "123456789-5c51509f-8c5c5dc2-b6557";
    std::string_view sv = api_key_;
    std::cout << "sv:" << sv << std::endl;

    std::pair<std::string_view, std::string_view> xx = std::make_pair<std::string_view, std::string_view>("AccessKeyId", api_key_);
    std::cout << "xx.second:" << xx.second << std::endl;

    std::pair<std::string_view, std::string_view> yy = std::make_pair("AccessKeyId", api_key_);
    std::cout << "yy.second:" << yy.second << std::endl;
}

compile and output:

$ g++ ./try2.cpp -std=c++17
$./a.out
sv:123456789-5c51509f-8c5c5dc2-b6557
xx.second:123456789-5c51509f-8c5c5dc2-b6557
yy.second:9-5c51509f-8c5c5dc2-b6557

Aucun commentaire:

Enregistrer un commentaire