Search code examples
c++visual-c++c++23explicit-object-parameter

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.


Solution

  • 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.