C++中的类型推断(auto 和 decltype)

类型推断 是指在编程语言中自动推导表达式的数据类型。在C++11之前,每个数据类型都需要在编译时显示声明,在运行时限制表达式的值,但在C++的新版本之后,引入了 auto 和 decltype等关键字,这允许程序员将类型推导留给了编译器本身。

有了类型推断功能,我们必须耗费时间去写编译器已经知道的东西。由于所有类型仅在编译器阶段推导,因此编译时间略有增加,但不会影响程序的运行时间。

1. C++中的auto

C++中的 auto 关键字指定要声明的变量的类型根据其初始值自动推导。如果函数的返回类型是auto,那么它将在运行时由返回类型表达式确定类型。auto的良好用途是在为容器创建迭代器时避免冗长的初始化。

注意:使用auto关键字声明的变量应仅在声明时初始化,否则将出现编译时错误。

C++中auto例子

cpp 复制代码
// C++ program to demonstrate working of auto
// and type inference

#include <bits/stdc++.h>
using namespace std;

int main()
{
	// auto a; this line will give error
	// because 'a' is not initialized at
	// the time of declaration
	// a=33;

	// see here x ,y,ptr are
	// initialised at the time of
	// declaration hence there is
	// no error in them
	auto x = 4;
	auto y = 3.37;
	auto z = 3.37f;
	auto c = 'a';
	auto ptr = &x;
	auto pptr = &ptr; //pointer to a pointer
	cout << typeid(x).name() << endl
		<< typeid(y).name() << endl
		<< typeid(z).name() << endl
		<< typeid(c).name() << endl
		<< typeid(ptr).name() << endl
		<< typeid(pptr).name() << endl;

	return 0;
}

输出

bash 复制代码
i
d
f
c
Pi
PPi

注意 :我们使用了 typeid 来获取变量的类型。

这里,typeid是一个运算符,用于获取需要对象的动态类型。

typeid(x).name() 返回 x 的数据类型,例如,它返回:

  • i 表整数integer,d 表 doubles
  • f 表 float,cchar
  • Pi 表指向整数的指针
  • Pd 表示指向double的指针
  • Pc 表示指向 char 的指针
  • PPi 表示指向整数的指针的指针
  • 单指针前缀 P
  • 双指针用 PP 作为前缀,以此类推

但实际返回的名称主要取决于编译器。

注意:auto的良好用途是在为容器创建迭代器时避免冗长的初始化。

C++ auto关键字例子

cpp 复制代码
// C++ program to demonstrate that we can use auto to
// save time when creating iterators

#include <bits/stdc++.h>
using namespace std;

int main()
{
	// Create a set of strings
	set<string> st;
	st.insert({ "geeks", "for", "geeks", "org" });

	// 'it' evaluates to iterator to set of string
	// type automatically
	for (auto it = st.begin(); it != st.end(); it++)
		cout << *it << " ";

	return 0;
}

输出

bash 复制代码
for geeks org 

注意:如果给auto赋值一个整型引用,它也会变成整型。要使其成为引用类型,我们使用auto &。

  • 返回整数引用类型的函数:int& fun() {};
  • m 会默认为 int类型而不是 int&类型:auto m = fun();
  • n 将是 int& 类型因为使用了auto &:auto& n = fun();

2. C++中的decltype

在C++中,decltype关键字可以检查实体声明的类型或表达式的类型。'auto' 允许你声明具有特定类型的变量,而decltype允许你从变量中提取类型,所以decltype是一种计算传递表达式类型的操作符。

下面解释上述关键字及其用途:

例子

cpp 复制代码
// C++ program to demonstrate use of decltype
#include <bits/stdc++.h>
using namespace std;

int fun1() { return 10; }
char fun2() { return 'g'; }

int main()
{
	// Data type of x is same as return type of fun1()
	// and type of y is same as return type of fun2()
	decltype(fun1()) x;
	decltype(fun2()) y;

	cout << typeid(x).name() << endl;
	cout << typeid(y).name() << endl;

	return 0;
}

输出

cpp 复制代码
i
c

以下是演示decltype的使用的又一个示例,

cpp 复制代码
// C++ program to demonstrate use of decltype
#include <bits/stdc++.h>
using namespace std;

// Driver Code
int main()
{
	int x = 5;

	// j will be of type int : data type of x
	decltype(x) j = x + 5;

	cout << typeid(j).name();

	return 0;
}

输出

bash 复制代码
i

示例:演示auto和decltype的使用的C++程序

下面是一个C++模板函数min_type(),它返回两个数字中的最小值。这两个数字可以是任何整数类型。返回类型是由两者中的最小值来确定的。

cpp 复制代码
// C++ program to demonstrate use of decltype in functions
#include <bits/stdc++.h>
using namespace std;

// A generic function which finds minimum of two values
// return type is type of variable which is minimum
template <class A, class B>
auto findMin(A a, B b) -> decltype(a < b ? a : b)
{
	return (a < b) ? a : b;
}

// driver function to test various inference
int main()
{
	// This call returns 3.44 of double type
	cout << findMin(4, 3.44) << endl;

	// This call returns 3 of double type
	cout << findMin(5.4, 3) << endl;

	return 0;
}

输出:

bash 复制代码
3.44
3

decltype vs typeid

以下是decltype和typeid之间的一些主要区别

  • decltype在编译时提供类型信息,而typeid在运行时提供。
  • 因此,如果我们有一个基类引用(或指针)引用(或指向)一个派生类对象,decltype会将类型作为基类引用(或者指针),但typeid会将类型作为派生类引用(或者指针)。
相关推荐
zyh2005043017 天前
c++碎片小知识
开发语言·c++·nullptr·for·auto
EleganceJiaBao3 个月前
【C语言】auto 关键字
c语言·关键字·语法·auto·类型
hope_wisdom8 个月前
C++ 11新特性之auto和decltype
c++·decltype·auto·c++ 11
Laurence9 个月前
HBase 创建不分裂的表 ( 禁止 Table Split )
hbase·split·auto·禁止·disable·预分区
Dream_Chaser~9 个月前
【C++初阶】第一站:C++入门基础(下)
c++·笔记·编程语言·vs2019·范围for·内联·auto
dvlinker1 年前
C++11新特性① | C++11 常用关键字实战详解
c++·关键字·nullptr·c++11新特性·=delete·decltype·noexcept