自动与字符串文本
auto with string literals
#include <iostream>
#include <typeinfo>
int main()
{
const char a[] = "hello world";
const char * p = "hello world";
auto x = "hello world";
if (typeid(x) == typeid(a))
std::cout << "It's an array!n";
else if (typeid(x) == typeid(p))
std::cout << "It's a pointer!n"; // this is printed
else
std::cout << "It's Superman!n";
}
当字符串文字实际上是数组时,为什么x
被推导为指针?
窄字符串文本的类型为">n
const char
数组" [2.14.5 字符串文本 [lex.string] §8]
基于模板参数推导的功能auto
和模板参数推导的行为相同,特别是根据 §14.8.2.1/2(C++11 标准(:
- 如果 P 不是引用类型
- 如果 A 是数组类型
- ,则使用数组到指针转换生成的指针类型代替 A 进行类型推断
如果您希望表达式x
的类型是数组类型,只需在auto
后添加&
:
auto& x = "Hello world!";
然后,auto
占位符将被推断为const char[13]
。这也类似于将引用作为参数的函数模板。为了避免任何混淆:声明的 x 类型将是引用数组。
当字符串文字实际上是数组时,为什么 x 被推导为指针?
因为数组到指针的转换。
如果要将x
推导为数组,则仅当允许以下情况时:
const char m[] = "ABC";
const char n[sizeof(m)] = m; //error
在C++中,一个arrray不能用另一个数组初始化(如上所示(。在这种情况下,源数组将衰减为指针类型,您可以改为执行此操作:
const char* n = m; //ok
使用 auto
进行类型推断的规则与函数模板中的类型推断规则相同:
template<typename T>
void f(T n);
f(m); //T is deduced as const char*
f("ABC"); //T is deduced as const char*
auto n = m; //n's type is inferred as const char*
auto n = "ABC"; //n's type is inferred as const char*
§7.1.6.4/6 说auto
说明符:
然后,为变量 d 推导的类型是使用从函数调用中推导的模板参数推导规则确定的 A (14.8.2.1( ...
如果你想把
x推导为数组,你可以使用
decltype(auto) x = "hello world";
相关文章:
- 在 C++ 中从 8 位 ASCII 字符创建 7 位 ASCII 文本字符串
- cin>>gender 和 cin>>*gender ( c 样式文本字符串)有什么区别
- 是否可以动态检查文本字符串是否是 C++ 中给定类的成员?
- 用Zlib解压缩文本字符串
- 将文本字符串作为常量字符 * 参数传递会导致代码分析器错误
- 来自文本字符串或某种其他机制的代码类自动生成器
- 函数读取最大和min int值,并用文本字符串返回
- 模板代码和文本字符串
- 声明具有常量引用与常量变量的常量文本字符串
- 加密给定的文本字符串-Caesar Cipher
- 如何使用键盘输入和sf ::文本在SFML中添加一种文本框以显示文本字符串
- 在C++代码中,如何让用户在2个文本字符串之间输入一个数字
- 如何将文本从一个文件复制到另一个文件,然后将文本字符串的第一个字母转换为大写
- 从文本字符串推断类型
- 跟踪内存中的文本字符串
- 将字符串变量与整数和文本字符串连接起来的 C++ 字符串流
- 将 char 数组分配给文本字符串 - C++
- 如何修复将文本字符串附加到 C 字符串的错误
- 使用C++读取不同长度的多个文本字符串
- 在C++中创建原始文本字符串,类似于 C# 的"@ string"