字符串文字类型不符合预期
String literal type is not as expected
考虑以下程序:
#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;
}
它 returns 只有零,而我本以为 "hello"
是 const char[6]
。 "hello"
的类型是什么?
编辑:我太快了,没有注意到 const char[6]
。由于 none 您的支票涉及 &
:
引用 8.4.1 Literals:
A literal is a primary expression. Its type depends on its form. A
string literal is an lvalue; all other literals are prvalues.
因此,类型是const char (&)[6]
。
类型是 const char (&)[6]
如您所见 here
考虑以下程序:
#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;
}
它 returns 只有零,而我本以为 "hello"
是 const char[6]
。 "hello"
的类型是什么?
编辑:我太快了,没有注意到 const char[6]
。由于 none 您的支票涉及 &
:
引用 8.4.1 Literals:
A literal is a primary expression. Its type depends on its form. A string literal is an lvalue; all other literals are prvalues.
因此,类型是const char (&)[6]
。
类型是 const char (&)[6]
如您所见 here