2-3. Using Type Deduction

Type Deduction 发生在编译时期

可以对一般类型,自定义类型进行类型自推导

下面有两个例子:

1. Using auto with a class

 1 #include <iostream>
 2 #include <typeinfo>
 3
 4 using namespace std;
 5
 6 class MyClass
 7 {
 8 };
 9
10 int main()
11 {
12     auto variable = MyClass();
13     cout << "Type of variable: " << typeid(variable).name() << endl;
14     return 0;
15 }

2.Using auto with Uniform Initialization

#include <iostream>
#include <typeinfo>
using namespace std;

class MyClass
{
};

int main()
{
    auto variable{ 1 };
    cout << "Type of variable: " << typeid(variable).name() << endl;
    auto variable2{ MyClass{} };
    cout << "Type of variable: " << typeid(variable2).name() << endl;
    return 0;
}

use auto for local variables as much as possible. 在局部变量的时候使用auto

时间: 2024-08-26 07:18:20

2-3. Using Type Deduction的相关文章

现代C++之理解模板类型推断(template type deduction)

理解模板类型推断(template type deduction) 我们往往不能理解一个复杂的系统是如何运作的,但是却知道这个系统能够做什么.C++的模板类型推断便是如此,把参数传递到模板函数往往能让程序员得到满意的结果,但是却不能够比较清晰的描述其中的推断过程.模板类型推断是现代C++中被广泛使用的关键字auto的基础.当在auto上下文中使用模板类型推断的时候,它不会像应用在模板中那么直观,所以理解模板类型推断是如何在auto中运作的就很重要了. 下面将详细讨论.看下面的伪代码: templ

[Effective Modern C++] Item 1. Understand template type deduction - 了解模板类型推断

条款一 了解模板类型推断 基本情况 首先定义函数模板和函数调用的形式如下,在编译期间,编译器推断T和ParamType的类型,两者基本不相同,因为ParamType常常包含const.引用等修饰符 template<typename T> void f(ParamType param); // 函数模板形式 f(expr); // 函数调用 存在T的类型即为expr类型的情况,如下T为int templat<typename T> void f(const T& param

C++ return type deduction

1 template <typename T> 2 class TD; 3 4 int& foo(){ 5 static int x=0; 6 return x; 7 } 8 9 int main(){ 10 11 auto lambda = []()->const auto&{ return foo(); }; 12 TD<decltype(lambda())> dummy; 13 } 第11行,返回值类型按照const auto& 模式推导.这是用

【C++注意事项】5 Top-level const , The auto and decltype Type Specifier

top-level const As we've seen, a pointer is an object that can point to a different object. As a result, we can talk independently about whether a pointer is const and whether the objects to which it can point are const. we use the top-level const to

《Effective Modern C++》翻译--条款3: 理解decltype

条款3:理解decltype decltype 是一个非常有趣的怪兽.如果提供了一个名字或是表达式,decltype关键字将会告诉你这个名字或这个表达式的类型.通常情况下,结果与你的期望吻合.然而有些时候,decltype产生的结果领你挠头,使你去翻阅参考书或在网上问答中寻求答案. 我们先从通常的情况开始-这里没有暗藏惊喜.联系到在模板类型推导和auto类型推导中发生了什么,decltype关键字就像鹦鹉学舌一样,对于变量名称或表达式类型的推导跟模板类型推导和auto类型推导没有任何变化: co

【C++】函数和指针

最近在看C++ primer plus,感觉函数与指针这一章难点比较多,记写笔记,加强理解. From C++ Primer Plus: Chapter 7 Function:C++ Programming Modules 1. 如何声明函数指针? 和函数原型类似: 需要声明指针指向函数的返回值和参数列表 double pam(int); //参数为int 类型,返回值为double 类型的函数 double (*pf);(int) //指向参数为int类型,返回值为double 类型的指针 p

居然还有这样使用的auto

今天学习了一下keyword,无意中发现了自己一直未曾接触到的auto 好吧,我又开始胡扯了! automatic storage duration. (deprecated) 1) When declaring variables in block scope, in namespace scope, in init statements of for loops, etc, the type of the variable may be omitted and the keyword aut

C++11标准之右值引用(rvalue reference)

1.右值引用引入的背景 临时对象的产生和拷贝所带来的效率折损,一直是C++所为人诟病的问题.但是C++标准允许编译器对于临时对象的产生具有完全的自由度,从而发展出了Copy Elision.RVO(包括NRVO)等编译器优化技术,它们可以防止某些情况下临时对象产生和拷贝.下面简单地介绍一下Copy Elision.RVO,对此不感兴趣的可以直接跳过: (1) Copy Elision Copy Elision技术是为了防止某些不必要的临时对象产生和拷贝,例如: struct A { A(int)

C++14 SFINAE 解引用迭代器

C++14 SFINAE 解引用迭代器 p { margin-bottom: 0.25cm; line-height: 120% } a:link { } 原问题:编写函数f(r),若r为迭代器,则返回f(*r),否则返回r. p { margin-bottom: 0.25cm; line-height: 120% } a:link { } 摘要: p { margin-bottom: 0.25cm; line-height: 120% } a:link { } 问题: 什么是迭代器? 迭代器是