I met a decltype() with two parameters as return value type of template function:
template<class C, class F>
auto test(C c, F f) -> decltype((void)(c.*f)(), void()) { }
Does someone knows what is the second parameter, void()?
Thank you very much.
In the expression
(void)(c.*f)(), void():(void)(c.*f)()serves to check thatfis a member function incthat can be called with no arguments; it doesn't matter what the member-function return type is anyway, but it's nominally cast tovoidif the above is valid, the comma operator discards it and considers the second part, such that the overall effect is per
decltype(void()), which yields avoidtypePraetorian comments below that the trailing
, void()is redundant, as the leading part is cast tovoid(the C-style cast(void)) anyway... I suspect the, void()is intended as documentation, highlighting theenable_if-like conditional selection of a return type, it's a style choice whether to shorten that further todecltype((c.*f)(), void()).Further details / example
This can be used for SFINAE, though
enable_ifis more self-documenting. Consider this code, and the comments inmain()(CT stands for Compile Time):Output:
You can see and run the code here.