009auto类型常规推断
#include<iostream>
#include<cstdlib>
#include <boost/type_index.hpp>
using namespace std;
template<typename T>
void myFunction01(T&tem)
{
using boost::typeindex::type_id_with_cvr;
cout << "T type=" << type_id_with_cvr<T>().pretty_name() << endl;
cout << "tem type=" << type_id_with_cvr<decltype(tem)>().pretty_name() << endl;
}
int main(void)
{
int number01 = 18;
const int number02 = number01;
const int&number03 = number01;
myFunction01(number01);
myFunction01(number02);
myFunction01(number03);
auto number04 = 23;
const auto&number07 = number01;
auto number08 = number07;
auto &number09 = number07;
const auto*p1 = &number01;
auto*p2 = &number01;
auto p3 = &number01;
auto&&number11 = number01;
const auto number12 = 234;
auto&&number13 = number12;
auto&&number14 = 234;
auto number05 = 234;
const auto number06 = number05;
auto number10 = new auto(2343);
system("pause");
return 0;
}
010auto针对数组和函数的推断
#include<iostream>
#include<cstdlib>
#include <boost/type_index.hpp>
using namespace std;
template<typename T>
void myFunction01(T&tem)
{
using boost::typeindex::type_id_with_cvr;
cout << "T type=" << type_id_with_cvr<T>().pretty_name() << endl;
cout << "tem type=" << type_id_with_cvr<decltype(tem)>().pretty_name() << endl;
}
void myFunction(double a,int b)
{
}
int main(void)
{
const char myStr[] = "jisuanjizuchengyaunli";
auto mystr02 = myStr;
auto&mystr03 = myStr;
int a[2] = { 1,2 };
auto myIntarry = a;
auto myf = myFunction;
auto &myf02 = myFunction;
using boost::typeindex::type_id_with_cvr;
cout << "myf type=" << type_id_with_cvr<decltype(myf)>().pretty_name() << endl;
cout << "myf02 type=" << type_id_with_cvr<decltype(myf02)>().pretty_name() << endl;
system("pause");
return 0;
}
011auto类型initializer_list的特殊推断
#include<iostream>
#include<cstdlib>
#include <boost/type_index.hpp>
using namespace std;
template<typename T>
void myFunction01(T&tem)
{
using boost::typeindex::type_id_with_cvr;
cout << "T type=" << type_id_with_cvr<T>().pretty_name() << endl;
cout << "tem type=" << type_id_with_cvr<decltype(tem)>().pretty_name() << endl;
}
int main(void)
{
int number01 = 10;
int number02(23);
int number03 = { 23 };
int number04{ 234 };
auto number05 = 23;
auto number06(23);
auto number07 = { 23 };
auto number08{ 23 };
using boost::typeindex::type_id_with_cvr;
cout << "number07 type=" << type_id_with_cvr<decltype(number07)>().pretty_name() << endl;
cout << "number08 type=" << type_id_with_cvr<decltype(number08)>().pretty_name() << endl;
auto number09 = { 23,345 };
system("pause");
return 0;
}
012auto不适用场合举例
#include<iostream>
#include<cstdlib>
#include <boost/type_index.hpp>
using namespace std;
template<typename T>
void myFunction01(T&tem)
{
using boost::typeindex::type_id_with_cvr;
cout << "T type=" << type_id_with_cvr<T>().pretty_name() << endl;
cout << "tem type=" << type_id_with_cvr<decltype(tem)>().pretty_name() << endl;
}
class TescClass
{
public:
static const auto m2 = 23;
};
int main(void)
{
system("pause");
return 0;
}
013auto适用场合举例
#include<iostream>
#include<cstdlib>
#include <boost/type_index.hpp>
#include <map>
#include <string>
using namespace std;
template<typename T>
void myFunction01(T&tem)
{
using boost::typeindex::type_id_with_cvr;
cout << "T type=" << type_id_with_cvr<T>().pretty_name() << endl;
cout << "tem type=" << type_id_with_cvr<decltype(tem)>().pretty_name() << endl;
}
class TescClassA
{
public:
static int test()
{
return 0;
}
};
class TescClassB
{
public:
static double test()
{
return 0.12;
}
};
template<typename T>
auto testFunction()
{
auto value = T::test();
return value;
}
int main(void)
{
std::map<string, int>myMap;
myMap.insert(make_pair("aaa", 3454));
myMap.insert({ "bbb",222 });
myMap.insert({ "ccc",3434 });
for (map<string,int>::iterator iter=myMap.begin();iter!=myMap.end();iter++)
{
cout << iter->first << "=" << iter->second << endl;
}
for (auto iter=myMap.begin();iter!=myMap.end();iter++)
{
cout << iter->first << "=" << iter->second << endl;
}
cout << testFunction<TescClassA>() << endl;
cout << testFunction<TescClassB>() << endl;
system("pause");
return 0;
}