std :: string类型丢失在元组中 [英] std::string type lost in tuple

查看:153
本文介绍了std :: string类型丢失在元组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释这里发生了什么吗? GCC 5.2的输出:

Can someone explain what's going on here? Output from GCC 5.2:

#include <iostream>
#include <tuple>
#include <string>
#include <typeinfo>

template <typename... Args>
void foo (Args&&... args) {
    std::tuple<Args...> t = std::tie(args...);
    std::cout << std::is_same<int, std::tuple_element_t<0, decltype(t)>>::value << '\n';  // true
    std::cout << std::is_same<std::string, std::tuple_element_t<1, decltype(t)>>::value << '\n';  // false
    std::cout << typeid(std::tuple_element_t<1, decltype(t)>).name() << '\n';  // A3_C (what's this???)
}

int main() {
    foo (5, "hi");
}

为什么std :: string类型丢失了,它变成了什么

Why is the std::string type lost, and what has it turn into instead?

推荐答案

字符串 -是 const char [N] 数组, std :: string 。在这种情况下, const char [] = {’h’,‘i’,‘\0’}-> const char [3]-> 3 类型为 char 的元素的数组 A3_C)。
使用 std :: string( hi)来创建字符串或启用用户文字并写入 hi s

"string" - is a const char[N] array, not std::string. In this case const char[] = {'h', 'i', '\0'} -> const char[3] -> Array of 3 elements of type char (A3_C). Use std::string("hi") to make string or enable user literals and write "hi"s

这篇关于std :: string类型丢失在元组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆