should I use decltype(auto) or auto for return type?

Jun 26, 2020 at 2:25pm
Hi,

I have this code.. I don't know if operator() should return decltype(auto) or auto.
What is the difference? Which should I use?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
template<typename CB>
class CountCalls
{
private:
	CB callback;
	long calls = 0;
public:
	CountCalls(CB cb) : callback(cb) {}
	template<typename ...Args>
	decltype(auto) operator()(Args&&...args)
	{
		++calls;
		return callback(std::forward<Args>(args)...);
	}
	long count() const
	{
		return calls;
	}
};



Regards,
Juan
Jun 26, 2020 at 2:39pm
decltype(auto), because your callback could return a cv-qualified value or reference type, and you need to be sure that your function call operator forwards whatever your callback returns to the user.
Jun 26, 2020 at 3:14pm
thanks!!
Topic archived. No new replies allowed.