VS2010中的C++0x特性

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

//自动类型 + 匿名函数
void Case1()
{
	vector<int> v;
	for (int i = 0; i < 10; ++i) 
	{
		v.push_back(i);
	}
	auto fun = [](int n) -> void { cout << n << " "; };
	for_each(v.begin(), v.end(), fun);
	cout << endl;
}

//自动类型 + 匿名函数 + 匿名函数构造参
void Case2()
{
	vector<int> v;
	for (int i = 0; i < 10; ++i) 
	{
		v.push_back(i);
	}

	int x = 5;
	int y = 6;
	auto fun = [x,y](int n) -> void { cout << n << "-" << x << "-" << y <<endl; };
	for_each(v.begin(), v.end(), fun);
	cout << endl;
}

//自动类型 + 匿名函数 + 匿名函数引用构造参
void Case3()
{
	vector<int> v;
	for (int i = 0; i < 10; ++i) 
	{
		v.push_back(i);
	}

	int x = 5;
	int y = 6;
	auto fun = [&x, &y](int n) -> void { cout << n << "-" << x << "-" << y <<endl; x++; y++; };
	for_each(v.begin(), v.end(), fun);
	cout << endl;
}

//自动类型 + 匿名函数 + 匿名函数引用构造参 + 友元常量对象
class Case4_Class
{
public:
	Case4_Class():m_toys(1){}
	void text(const vector<int>& v) const 
	{
		for_each(v.begin(), v.end(), [this](int n) 
		{
			cout << this->m_toys << endl;
		});
	}

private:
	int m_toys;
};
void Case4()
{
	vector<int> v;
	for (int i = 0; i < 10; ++i) 
	{
		v.push_back(i);
	}
	Case4_Class c4;
	c4.text(v);
}

//编译器逻辑查验
void Case5()
{
	const bool ret = false;
	//static_assert(ret, "Case5 requires N < 2.");
}

//类型推算 + 完美转发
class Case6_Class
{
public:
	template <typename T, typename U>
	auto operator()(T&& t, U&& u) ->decltype( std::forward<T>(t) + std::forward<U>(u))
	{
		return std::forward<T>(t) + std::forward<U>(u);
	}
};
void Case6()
{
	cout<< Case6_Class()(1,2)<<endl;;
}

//外部模板




void main()
{
	Case6();
	system("pause");
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值