3

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.

0

1 Answer 1

9

In the expression (void)(c.*f)(), void():

  • (void)(c.*f)() serves to check that f is a member function in c that can be called with no arguments; it doesn't matter what the member-function return type is anyway, but it's nominally cast to void

  • if 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 a void type

Praetorian comments below that the trailing , void() is redundant, as the leading part is cast to void (the C-style cast (void)) anyway... I suspect the , void() is intended as documentation, highlighting the enable_if-like conditional selection of a return type, it's a style choice whether to shorten that further to decltype((c.*f)(), void()).

Further details / example

This can be used for SFINAE, though enable_if is more self-documenting. Consider this code, and the comments in main() (CT stands for Compile Time):

#include <iostream>

template<class C, class F>
auto test(C c, F f) -> decltype((void)(c.*f)(), void())
    { std::cout << "member function\n"; }

template<class C>
void test(C c, int)
    { std::cout << "int\n"; }

struct X {
    int f() { return 42; }
    double g(int) { return 3.14; }
};

int main()
{
    X x;
    test(x, &X::f);  // ok - outputs "member function\n"
    // test(x, &X::g);  // CT error - g needs an argument
    test(x, 99);   // ok - outputs "int\n"
}

Output:

member function
int

You can see and run the code here.

9
  • Thank you for answering it, Tony D. It seems decltype checks c has a member function void f(). Please let me confirm the part of "if the above is valid". Is it "if the above is invalid"? I am confused because both parameter returns void. If I misunderstand anything. I apologize for confirming above.Thank you very much.
    – mora
    Jun 24, 2015 at 3:45
  • 1
    "both parameter returns void" Eh? Jun 24, 2015 at 3:49
  • 2
    The , void() part is redundant since the return type of the function is being cast to void anyway
    – Praetorian
    Jun 24, 2015 at 3:54
  • 1
    return type of (void)(c.*f)() and void() are void. If (void)(c.*f)() is valid, why it does not apply decltype( (void)(c.*f)() ) ? That confuses me. I am beginner of c++. So I am afraid I am asking something strange. If so, please forgive me. Thank you very much.
    – mora
    Jun 24, 2015 at 3:56
  • 1
    I learned that the first augment is a valid checker for SFINAE. Now I understand why decltype() needs to have two parameters, valid checker (=the first augment) and return type(=the second augment). Thank you very much everyone.
    – mora
    Jun 24, 2015 at 4:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.