3

I use C++ https://cppinsights.io/ to see the progress of instantiation, and there's something puzzled between Function&& and Function.

I comment the code generated by cppinsights.

template<typename Function>
void bind(int type, Function&& func)
{
}

/*
// instantiated from the function above:  
template<>
inline void bind<void (*)()>(int type, void (*&&)() func)
{
}
*/

template<typename Function>
void bindtwo(int type, Function func)
{
}

/*
template<>
inline void bindtwo<void (*)()>(int type, void (*func)())
{
}
*/

void test()
{
    std::cout << "test" << std::endl;
}

int main()
{
    bind(1, &test);
    bindtwo(2, test);
}
Meepo
  • 33
  • 4
  • It is just a rvalue reference to a function pointer. (It applied the `&&` to the template parameter as expected.) It is not clear to me what it is that you are confused about and need an answer for. Can you please elaborate? – user17732522 Aug 11 '22 at 07:39
  • Related: [c++ r-value reference applied to function pointer](https://stackoverflow.com/questions/60366121/c-r-value-reference-applied-to-function-pointer) – Jason Aug 11 '22 at 07:42
  • It's it your random code? 1. Syntax errors. 2. `void (*&&func)()` does not have any practical use, similar to `int&&`. – 273K Aug 11 '22 at 07:55
  • @273K The explicit specializations are automatically generated by the tool OP is referencing. It is intended to show how the implicit instantiations created from the calls in `main` look as a learning and diagnosis tool. It is unfortunate that it produces code with syntax errors, but it is not meant to ever be compiled anyway. – user17732522 Aug 11 '22 at 07:57
  • @user17732522 Thanks, will know it. It would be better if OP had posted the original source that resulted in those insights. – 273K Aug 11 '22 at 08:00
  • *"there's something puzzled between Function&& and Function."* -- what specifically is puzzled? A question without enough text to be found via a search is of no value to the next person with the same question, hence is not of value to Stack Overflow. – JaMiT Aug 11 '22 at 11:09
  • @JaMiT sry , just as the title of question. – Meepo Aug 11 '22 at 13:43
  • @Meepo Huh? The title asks how to understand something. That does not explain what the puzzle is between that something and something else. You presented "Function&& and Function" to either contrast them or compare them (I don't know which). What did you expect us to see? – JaMiT Aug 12 '22 at 03:34

1 Answers1

5

how to understand void (*&&)() func

First things first, the above syntax is wrong because func should appear after the && and not after the () as shown below.

Now after the correction(shown below), func is an rvalue reference to a pointer to a function that takes no parameter and has the return type of void.

Note also that the syntax void (*&&)() func is wrong, the correct syntax would be as shown below:

template<>
//----------------------------------------------vvvv----->note the placement of func has been changed here
inline void bind<void (*)()>(int type, void (*&&func)() )
{
}
Jason
  • 36,170
  • 5
  • 26
  • 60
  • sry, I have commented the code generated by cppinsights. I just want to see the progress of instantiation – Meepo Aug 11 '22 at 08:08
  • @Meepo It's fine, cppinsights is a good way to test your assumptions. You were asking about `void (*&&)()` which is an *rvalue reference to a pointer to a function that has no parameter and has return type of void*. – Jason Aug 11 '22 at 08:11