Consider the following struct:
struct S {};
In C++14, the definition below is valid:
constexpr auto f() { return S{}, 'c'; }
As well as the following one:
constexpr auto f() { return S{}, void(); }
Now, consider the following, working snippet that involves the first of the two definitions:
#include<type_traits>
struct S {};
constexpr int operator,(S, char) { return 42; }
constexpr auto f() { return S{}, 'c'; }
int main() {
constexpr int i{f()};
static_assert(i == 42, "!");
static_assert(std::is_same<decltype(f()), int>::value, "!");
}
Speaking not so technically, the overload of the comma operator intercepts the couple S{}, 'c'
and returns an integer, as correctly verified in the main
function.
Now, suppose I want to do the same with the second definition of f
:
constexpr auto f() { return S{}, void(); }
In this case, the comma operator should intercept the form S{}, void()
.
Neither the following definition works (for obvious reasons):
constexpr int operator,(S, void) { return 42; }
Nor the one below (that would have worked in the previous case):
template<typename T> constexpr int operator,(S, T &&) { return 42; }
Is there any way to overload the comma operator so as to deal with S{}, void()
?
Isn't it otherwise a lack in the standard, for it allows to use the comma operator that way, but doesn't give you the chance to overload the same operator (even if the standard mentions that overloaded functions involving S
are allowed)?
Note: this question is made for the sake of curiosity. Please, avoid comments like do not do that or it is not good practice. I'm not planning to do that in production environments. Thank you.