字符串文字类型不符合预期

时间:2018-02-13 22:28:41

标签: c++ c++11 char string-literals decltype

考虑以下计划:

#include <iostream>
#include <type_traits>

int main(int argc, char* argv[])
{
    std::cout << std::is_same<decltype("hello"), char*>::value << std::endl;
    std::cout << std::is_same<decltype("hello"), const char*>::value << std::endl;
    std::cout << std::is_same<decltype("hello"), char[5]>::value << std::endl;
    std::cout << std::is_same<decltype("hello"), const char[5]>::value << std::endl;
    std::cout << std::is_same<decltype("hello"), char[6]>::value << std::endl;
    std::cout << std::is_same<decltype("hello"), const char[6]>::value << std::endl;
    std::cout << std::is_same<decltype("hello"), char[]>::value << std::endl;
    std::cout << std::is_same<decltype("hello"), const char[]>::value << std::endl;
    return 0;
}

它只返回零,而我希望"hello"const char[6]"hello"的类型是什么?

2 个答案:

答案 0 :(得分:3)

编辑:我太快了,没有注意到const char[6]。由于您的检查都不涉及&

引用8.4.1 Literals

  

文字是主要表达方式。它的类型取决于它的形式。一个   字符串文字是lvalue;所有其他文字都是prvalues。

因此,类型为const char (&)[6]

答案 1 :(得分:1)

类型为const char (&)[6],您可以看到here