#include <vector>
#include <map>
using namespace std;
int main(int argc, char *argv[], char *env[])
{
// auto a; // 错误,没有初始化表达式,无法推断出a的类型
// auto int a = 10; // 错误,auto临时变量的语义在C++11中已不存在, 这是旧标准的用法。
// 1. 自动帮助推导类型
auto a = 10;
auto c = 'A';
auto s("hello");
// 2. 类型冗长
map<int, map<int,int> > map_;
map<int, map<int,int>>::const_iterator itr1 = map_.begin();
const auto itr2 = map_.begin();
auto ptr = []()
{
std::cout << "hello world" << std::endl;
};
return 0;
};
// 3. 使用模板技术时,如果某个变量的类型依赖于模板参数,
// 不使用auto将很难确定变量的类型(使用auto后,将由编译器自动进行确定)。
template <class T, class U>
void Multiply(T t, U u)
{
auto v = t * u;
}
template <typename T1, typename T2>
auto compose(T1 t1, T2 t2) -> decltype(t1 + t2)
{
return t1+t2;
}
auto v = compose(2, 3.14); // v's type is double
auto k = 5; auto* pK = new auto(k); auto** ppK = new auto(&k); const auto n = 6;
auto m; // m should be intialized
auto int p; // 这是旧auto的做法。
void MyFunction(auto parameter){} // no auto as method argument
template<auto T> // utter nonsense - not allowed
void Fun(T t){}
int* p = new auto(0); //fine int* pp = new auto(); // should be initialized auto x = new auto(); // Hmmm ... no intializer auto* y = new auto(9); // Fine. Here y is a int* auto z = new auto(9); //Fine. Here z is a int* (It is not just an int)
int value = 123; auto x2 = (auto)value; // no casting using auto auto x3 = static_cast<auto>(value); // same as above
auto x1 = 5, x2 = 5.0, x3='r'; // This is too much....we cannot combine like this
const int i = 99; auto j = i; // j is int, rather than const int j = 100 // Fine. As j is not constant // Now let us try to have reference auto& k = i; // Now k is const int& k = 100; // Error. k is constant // Similarly with volatile qualifer
int a[9]; auto j = a; cout<<typeid(j).name()<<endl; // This will print int* auto& k = a; cout<<typeid(k).name()<<endl; // This will print int [9]
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有