It seems that you want to “copy” the signature of another function in order to create a function with the same signature. Since decltype(foo)
indeed a type of function (and not a pointer to that function, which would be decltype(&foo)
and lead to a pointer declaration), you can use it to declare a function with the same signature as another function.
As indicated by the linker error:
undefined reference to `Bar::blubb(int)'
this will work great with your compiler. However, it seems that gcc has not yet fully implemented this part of the standard, since it will not accept the syntax for the same using the function call operator. Klang by the way. happy to accept it, and then the connection with the errors
undefined reference to `Bar::operator()(int)'
Your question about why this linker error exists indicates a misunderstanding of what decltype really does.
He will simply appreciate the type, not more. The blubb
definition is blubb
no way associated with the foo
definition. It can be clearer when writing how
typedef decltype(foo) x; x blubb;
Now you can, as an alternative to typedef x, explicitly indicate the type of function that will in no way change what blubb is. You still need to define it. And since there is no syntax to define it using the decltype method, you obviously need to write it as
int Bar::operator()(int) { ... }
which, most likely, and, unfortunately, will lead to a defeat of the purpose / advantage of using decltype for the declaration, since it will not allow you to automatically "copy" the signature.