cpp11的自动类型推导
时间: 2023-06-01 18:02:40 浏览: 77
C++11引入了auto关键字,可以用于自动推导变量类型。auto关键字可以用于函数返回值、变量声明、迭代器声明等。在使用auto时,编译器会根据表达式的类型自动推导出变量的类型。例如:
```c++
auto i = 1; // 推导出i的类型为int
auto d = 3.14; // 推导出d的类型为double
auto s = "hello"; // 推导出s的类型为const char*
```
在函数返回值中使用auto时,需要使用尾置返回类型(trailing return type):
```c++
auto add(int a, int b) -> int // 尾置返回类型
{
return a + b;
}
```
auto关键字还可以和decltype关键字一起使用,用于推导表达式的类型:
```c++
int a = 1;
decltype(a) b = 2; // 推导出b的类型为int
auto c = a + b; // 推导出c的类型为int
```
相关问题
cpp11的所有新特性
1. 自动类型推导
2. 列表初始化
3. foreach循环
4. lambda表达式
5. 右值引用和移动语义
6. 委托构造函数
7. 默认和删除函数
8. constexpr函数和常量表达式
9. 变长参数模板
10. 线程支持库
11. std::unique_ptr和std::shared_ptr
12. std::function
13. std::tuple和std::make_tuple
14. std::array和std::initializer_list
15. std::atomic
16. static_assert
17. override和final关键字
18. Unicode字符串支持
19. Null指针常量
20. Range-Based for循环
21. Move semantics
22. Variadic templates
23. Type traits
24. Lambda expressions
25. Uniform initialization
26. Delegating constructors
27. Explicit conversion operators
28. std::chrono
29. std::condition_variable_any
30. std::future和std::promise
31. Thread-local storage
32. std::regex
33. std::unordered_map和std::unordered_set
34. std::to_string和std::stoi等函数
35. std::forward和std::move函数
36. std::tie函数
37. std::enable_if和std::is_same等类型判定函数
cpp11有哪些新特性
1. auto关键字:可以自动推导变量类型。
2. lambda表达式:可在函数内部定义匿名函数。
3. 右值引用和移动构造函数:可以提高代码效率。
4. nullptr关键字:表示空指针。
5. range-based for循环:可以遍历容器元素。
6. constexpr关键字:可以在编译时对表达式进行求值。
7. 委托构造函数:可以在构造函数中调用其他构造函数。
8. override关键字:用于表示函数覆盖基类的虚函数。
9. final关键字:用于表示类或虚函数不能被派生类或子类重写。
10. 智能指针:shared_ptr、unique_ptr和weak_ptr。
11. Unicode字符串和原始字符串字面值。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)