Lambda returning decltype(auto) vrs auto

Hi,

What is the difference between returning decltype(auto), auto and not specifyiung return type at all in a lambda?

I have this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
template<typename First, typename... Args>
void printLambdaDecltype(const First& f, const Args& ...args)
{
	cout << f;
	(cout << ... << [](const auto& arg) -> decltype(auto)
	{
			cout << ' ';
			return arg;
	}(args)) << endl;

}

template<typename First, typename... Args>
void printLambdaAuto(const First& f, const Args& ...args)
{
	cout << f;
	(cout << ... << [](const auto& arg) -> auto
	{
			cout << ' ';
			return arg;
	}(args)) << endl;

}

template<typename First, typename... Args>
void printLambdaNoReturnSpecified(const First& f, const Args& ...args)
{
	cout << f;
	(cout << ... << [](const auto& arg)
	{
			cout << ' ';
			return arg;
	}(args)) << endl;

}


?
Regards,
Juan
I think the decltype(auto) lambda returns const auto&;
the auto lambda returns just auto (no reference nor constant) - but a copy;
the not specified returns the same as auto (will return a copy of arg)
You asked this already: http://www.cplusplus.com/forum/general/188645/

EDIT: In fact, this is the third time you ask this question: http://www.cplusplus.com/forum/general/186478/
Maybe try using the search function?
Last edited on
auto never deduces to a reference. decltype(auto) can deduce to a reference. It returns a prvalue type (auto) if return value is of type rvalue and an lvalue type (auto &) if the return value is of type lvalue.

It's used with a templated function when the return is based upon the template type. Basically, value in, value out - ref in, ref out.
Last edited on
yes I had asked something similar. In this case I am asking for a particular situation, to wit:

Is the following true:


I think the decltype(auto) lambda returns const auto&;
the auto lambda returns just auto (no reference nor constant) - but a copy;
the not specified returns the same as auto (will return a copy of arg)


?
the return types of those three lambdas are const T&, T, and T.
Last edited on
thanks!!
Topic archived. No new replies allowed.