问题
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