What does the 'void()' in 'auto f(params) -> decltype(..., void())' do?

C++C++11SfinaeDecltypeTrailing Return-Type

C++ Problem Overview


I found code here that looked something like this:

auto f(T& t, size_t n) -> decltype(t.reserve(n), void()) { .. }

In all the documentation I read I was told that decltype is signed as:

> decltype( entity )

or

> decltype( expression )

And there is no second argument anywhere. At least that's what's pointed to on cppreference. Is this a second argument to decltype? And if so, what does it do?

C++ Solutions


Solution 1 - C++

Since it is an expression that comma is simply the comma operator (meaning the type is the type of the rhs side: void), not another argument.

That code is using SFINAE - it's enabled if t.reserve(n) exists but it wants to keep the return type as void.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontemplate boyView Question on Stackoverflow
Solution 1 - C++PubbyView Answer on Stackoverflow