2

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.

2
  • 3
    This has nothing to do with deduced this. A deduced this is just a specialized a kind of a template. It appears that the shown code attempts to declare a map containing a signature with a deduced this, i.e. a template. You cannot store templates in containers. C++ does not work this way, on a fundamental level. You'll run into the same problem attempting to declare a std::map<std::string, template<typename ptr> x>. It's unclear what the attempted goal of the shown code is, but whatever it is, a completely different approach will be needed. Commented Jan 10, 2024 at 23:53
  • 1
    "this" is a variable not a type. You don't put variables in types unless you're decltype-ing with it Commented Jan 11, 2024 at 4:47

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

0

You cannot use explicit object parameters in a free function because it just wouldn’t make sense (what will this be deduced to then)?

template <class s>
using FunctionMap = std::map<std::string, void(*)(this s& self)>; <——- pointer to free function stored in an std::map.

But deducing this will make more sense here:

class base{
//...
public:
   ///Get Map of fPtr
   template <class Self>
   FunctionMap<Self>* get_scripts(this const Self& self); <——- makes sense here doesn’t it? this is deduced to base.
}

And if:

class derived : public base {} <——- this is deduced to derived.

1 Comment

I know no one asked but just in case: ”A pointer to a member function with explicit object parameter is an ordinary pointer to function, not a pointer to member”

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.