阅读背景:

Apple LLVM3.1中的Blocks与GNU++11的兼容情况

来源:互联网 

测试结果非常可喜~

//
//  hello.mm
//  objCTest
//
//  Created by zenny_chen on 12-2-21.
//  Copyright (c) 2012年 GreenGames Studio. All rights reserved.
//

#import <Foundation/Foundation.h>

#include <iostream>
#include <functional>
using namespace std;

template <typename T>
void GeneralLambda4Type(T t)
{
    __block T var = T(100);
    
    // The type of the Block is: T(^)(T)
    auto a = ^(T i) {
        cout << "The value is: " << i << endl;
        var += i;
        return i + 1;
    };
    
    auto b = a(t);
    
    cout << "The returned value is: " << b << endl;
    
    cout << "The var is: " << var << endl;
}

template <typename T>
void GeneralLambda(void)
{
    T lam = ^(void){ };
    lam();
}

template <typename T>
void GeneralLambdaType(void (^lam)(T), const T &param)
{
    cout << "The parameter is: " << param << endl;
    
    // 函数对象也适用
    function<void(T)> func = lam;
    
    func(param);
}

// Block不能作为模板的非类型形参
//template <void (^LAMBDA)(int)>

extern "C" void HelloTest(void);

void HelloTest(void)
{
    GeneralLambda4Type(100);
    GeneralLambda<void(^)(void)>();
    GeneralLambdaType(^(int param){ cout << "The value is: " << param << endl; },
                      -100);
}
//
//  hello.mm
//  objCTest
//



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: