Lambda Constructor Mismatch in GCC / MSVC

Which one, if not both, violates the specification? Tried MSVC on both MSVC 2013 and MSVC Nov 2013 CTP, GCC was MinGW x64 4.9.1 with -std = C ++ 11.

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];
};

This is similar to how they handle lambda types in different ways, and when they consider a variable to be defined.

+4
source share
1 answer

GCC is right.

For your first form:

, . . int i = i;, i () .

:

= , . ( - .)

struct A { };
struct B { B(A) { } };
struct C { C(B) { } };
A a;
C c1(a); // okay
C c2 = a; // error
+5

All Articles