哪一個,如果不是兩個,都打破規範?在MSVC 2013和MSVC Nov 2013 CTP上使用MSVC,GCC爲MinGW x64 4.9.1,使用-std = C++ 11。GCC/MSVC中λ轉換構造函數的差異
template<typename ret_type>
class memoizer
{
using func_type = ret_type(*)(const int);
const func_type func;
std::map<int, ret_type> cache;
public:
memoizer(func_type func) : func(func)
{
}
ret_type operator [](const int n)
{
const auto it = cache.find(n);
if(it != cache.end())
return it->second;
return cache[n] = func(n);
}
};
//works in GCC but not MSVC
//error C2065: 'fib' : undeclared identifier
memoizer<int64_t> fib([](const int n)
{
return n < 2 ? n : fib[n - 1] + fib[n - 2];
});
//works in MSVC but not GCC
//error: conversion from '<lambda(int)>' to non-scalar type 'memoizer<long long int>' requested
memoizer<int64_t> fib = [](const int n)
{
return n < 2 ? n : fib[n - 1] + fib[n - 2];
};
這似乎源於他們處理lambda類型的方式不同,並且他們考慮定義一個變量。
相關:https://stackoverflow.com/questions/2067988/recursive-lambda-functions-in-c0x – 2014-10-06 19:03:47