Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it a syntax error to use explicit object parameters in a function pointer?

So I've run into a problem when trying to use explicit object parameters for a function pointer, for a project I'm working on. The code at a glance seems syntactically fine but as this is a new feature which I can't find too much information online, I am at a loss as to why I get random syntax errors. Specifically the error is "syntax error: ')'"

My only real guess is that it's some sort of MSVC incompleteness/bug in which case I will probably have to switch to a different compiler.

///base.h
///...
#include <map>

template <class s>
using FunctionMap = std::map<std::string,void(*)(this s& self)>;
                                            ///^ Syntax Error Here

class base{
//...
public:
   ///Get Map of fPtr
   template <class Self>
   FunctionMap<Self>* get_scripts(this const Self& self);
}

I expected this to compile but it keeps on coming up with the syntax error. I know explicit object parameters are only partially implemented in MSVC.

like image 258
Nathaniel Burchell Avatar asked Dec 07 '25 07:12

Nathaniel Burchell


2 Answers

Explicit object parameters in function pointers make no sense. This is not an MSVC issue, it's simply not valid code. A free function pointer cannot have any object parameter, be that implicit or explicit.

However, note the following:

  • void(*)(s& self) would be just fine, without the this
  • taking the address of an explicit object member function yields a regular function pointer
    • e.g. &base::get_scripts<T> is of type std::map<...>(*)(s&)

In short, you can just use regular function pointers. Simply get rid of the this in the function pointer type.

like image 53
Jan Schultke Avatar answered Dec 09 '25 03:12

Jan Schultke


An explicit object parameter is a concept that is meaningful only to a member function (specifically, it means that you can use them with member function call syntax, like m.func() or similar, and the given object will be transformed into the explicit object parameter). Once that member function gets converted to a function pointer, all of the magic of having an "explicit object parameter" goes away. It's just a function pointer; the first parameter has no special powers and there's no special call syntax that you can use with it.

The function pointer's signature is what you'd get if you remove this from the member function's signature.

like image 42
Nicol Bolas Avatar answered Dec 09 '25 02:12

Nicol Bolas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!