VS2010对c++11的支持情况验证

目前仅仅测试工作中 使用的比较多的:

智能指针

  1. shared_ptr

    #include <memory>
    std::shared_ptr<A> a(new A);

    ----支持! 同时也支持 make_shared

  2. weak_ptr

    ----支持,毕竟这是个给shared_ptr打辅助的指针模板

  3. unique_prt

    ----支持! ,但不支持make_unique,这也正常,毕竟这是C++14的语法了。

综合来看,可以在VS2010里自有的使用智能指针了。

auto类型自推导

vector<int> v_ints;
v_ints.push_back(1);
v_ints.push_back(2);
auto it = v_ints.cbegin();

std::cout << *it <<std::endl; 

auto a = make_shared<A>();

auto b = a;

------支持!

lambda表达式

Constructs a closure: an unnamed function object capable of capturing variables in scope.

c++11中有以下三种语法:

[ captures ] ( params ) -> ret_type { body } (1)
[ captures ] ( params ) { body } (2)
[ captures ] { body } (3)

理论上都是第一种语法的简化版,根据需要使用,

闭包是带有上下文的函数。说白了,就是有状态的函数

函数是代码, 状态是一组变量 ,将代码和一组变量捆绑 (bind) , 就形成了闭包。

C++中实现闭包的三种方式

  • 重载 operator()
    因为闭包是一个函数+一个状态, 这个状态通过 隐含的 this 指针传入. 所以 闭包必然是一个函数对象. 因为成员变量就是极好的用于保存状态的工具, 因此实现 operator() 运算符重载, 该类的对象就能作为闭包使用. 默认传入的 this 指针提供了访问成员变量的途径.(事实上, lambda 和 bind 的原理都是这个.)
class MyFunctor
{
public:
    MyFunctor(float f) : round(f) {}
    int operator()(float f) { return f + round; }
private:
    float round;
};
float round = 0.5;
MyFunctor f(round);
  • lambda表达式

    float round = 0.5;
    auto f = [=](float f) { return f + round; }

​ C++11里面的lambda就是闭包的实现。

  • boost::bind/std::bind

    int boost_func(float f, float round)
    { return f + round; }
    float round = 0.5;
    boost::function<int(float)> f = boost::bind(boost_func, _1, round);

closure的状态特指其运行的上下文。 closure将存贮它运行时需要的上下文,从而保证在closure创建时的上下文可以在closure运行时依然有效。

比如round就是closure的上下文。保存上下文的这一特点通常被称作“capture”或者是”bind”。 capture可以自己写,比如MyFuctor f(round); 也可以用boost::bind。

vs2010测试:

int make_i = 3;

auto f = [=](int x){
    std::cout << x << make_i << '\n'; 
};//这里的;不能省略

f(make_i);

------- 支持!

支持匿名表达式 ,则意味着闭包的支持,在C++里,闭包不是那么出名,毕竟这个概念是前端JS造出来的。但是清楚lambda表达式,我们可以精简代码,后面有计划补充学习记录。可以先参考这里

for_each

Possible implementation

template<class InputIt, class UnaryFunction>
UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f)
{
    for (; first != last; ++first) {
        f(*first);
    }
    return f;
}

循环遍历所有元素,使用f函数,对所有元素(迭代器)进行处理。

测试 for_each 里的lambda表达式

#include <algorithm>
for_each(v_ints.begin(),v_ints.end(),[](int val){
    cout << val << endl;
});

------ 支持!

Range-based for loop

vector<int> v_ints;
v_ints.push_back(1);
v_ints.push_back(2);

for (const int& i : v_ints) // access by const reference
    std::cout << i << ' ';
std::cout << '\n';

编译出错。

----不支持 !

正则表达式

测试:

std::string fnamesstring[] = {"foo.txt", "bar.txt", "baz.dat", "zoidberg"};
vector<string> fnames(fnamesstring,fnamesstring+4);
std::regex txt_regex("[a-z]+\\.txt");

for_each(fnames.begin(),fnames.end(),[&](const string &fname){
    std::cout << fname << ": " << std::regex_match(fname, txt_regex) << '\n';
}  );

-------支持!

bind函数模板

The function template bind generates a forwarding call wrapper for f. Calling this wrapper is equivalent to invoking f with some of its arguments bound to args.

---- 函数模板绑定为f生成一个转发调用包装器。调用这个包装器等价于调用f并将它的一些参数绑定到args。(机器翻译很感人)

个人理解,bind函数模板,可以更大范围的使用某些函数,比如嵌套绑定 子表达式函数,绑定类成员函数,甚至类的成员变量。

验证代码:

#include <functional>
void f(int n1 ,int n2,int n3 ,int n4 ,int n5){
    cout << n1 <<" "<< n2 <<" "<<n3 <<" "<<n4<<" "<<n5<<'\n';
}


using namespace std::placeholders;

void f(int n1 ,int n2,int n3 ,int n4 ,int n5);

auto b = bind(f,2,_1,_2,4,5);
b(100,200);

-------支持!

其他

vector<int> v_ints = {1,2,3,4,5,6,2,4,3};vs2010不支持这样初始化

可行的修改方案:

int ints[] = {1,2,3,4,5,6,2,4,3};
vector<int> v_ints(ints,ints+9);

猜你喜欢

转载自www.cnblogs.com/Stultz-Lee/p/10036207.html
今日推荐