2.4.1 auto


Contents

Name

auto

Description

The auto keyword tells the compiler to automatically deduce the data type of a variable from its initialization expression at compile time.

Remark

Examples

EX1

void auto_ex()
{
	auto v1 = 1; // int
	auto v2 = 3.14; // double
	auto v3 = "hello"; // const char*
	//auto v5 = 1, v6 = 3; // prevented, should not pass
}

EX2

vector GetVector() {vector vv;return vv;}
void auto_ex2()
{
	auto v7 = GetVector(); // vector, copy constructor
	//Similarly
	//vector& GetVectorRef();
	//auto& v8 = GetVectorRef(); // vector&
}

Header to Include

See Also

Reference