I'm trying to construct a lambda that returns by const int
, but the const
qualifier is being discarded. In the following code, lam1
and lam3
are behaving as expected, but lam2
is not. Expected output is 0 1 1
but I get 0 0 1
.
#include<type_traits>
#include<iostream>
using namespace std;
int main()
{
int C{0};
auto lam1 = [&]()-> int& {return C;};
auto lam2 = [&]()-> const int {return C;};
auto lam3 = [&]()-> const int& {return C;};
cout << is_const<remove_reference<decltype(lam1())>::type>::value << endl;
cout << is_const<remove_reference<decltype(lam2())>::type>::value << endl;
cout << is_const<remove_reference<decltype(lam3())>::type>::value << endl;
}
demo link: https://godbolt.org/z/xP4lYs