std::string type lost in tuple

喜夏-厌秋 提交于 2019-12-12 03:28:15

问题


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");
}

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


回答1:


"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




回答2:


"hi" (string literal) is of the type const char[3], not std::string



来源:https://stackoverflow.com/questions/36223914/stdstring-type-lost-in-tuple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!