c++ - Discrepancy with lambda conversion constructor in GCC/MSVC -
c++ - Discrepancy with lambda conversion constructor in GCC/MSVC -
which one, if not both, breaking spec? tried msvc on both msvc 2013 , msvc nov 2013 ctp, gcc mingw x64 4.9.1 -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 = cache.find(n); if(it != cache.end()) homecoming it->second; homecoming cache[n] = func(n); } }; //works in gcc not msvc //error c2065: 'fib' : undeclared identifier memoizer<int64_t> fib([](const int n) { homecoming n < 2 ? n : fib[n - 1] + fib[n - 2]; }); //works in msvc not gcc //error: conversion '<lambda(int)>' non-scalar type 'memoizer<long long int>' requested memoizer<int64_t> fib = [](const int n) { homecoming n < 2 ? n : fib[n - 1] + fib[n - 2]; };
this seems stem way they're handling lambda types differently, , when consider variable defined.
gcc right.
for first form:
a variable considered declared @ end of declarator, before initialiser. why form valid. famous illustration int = i;
, syntactically valid , initialises i
own (indeterminate) value.
for sec form:
your initialisation =
fails because have 2 user-defined conversions. (the lambda type's conversion operator considered user-defined.) analogous to
struct { }; struct b { b(a) { } }; struct c { c(b) { } }; a; c c1(a); // okay c c2 = a; // error
c++ visual-c++ gcc c++11
Comments
Post a Comment