ciprianr
ciprianr

Reputation: 309

Using pointer functions in factory generates compile time error

I have the following using declaration

template<typename T> using createShapeFunction = Shape<T>(*)(void);

This is for a factory so when I define the createShape() method I use the following syntax:

createShapeFunction<T>* function = creationFunctions.at(nameOfType);
Shape<T>* returnShape = *function();

Now this gives me the error:

error C2064: term does not evaluate to a function taking 0 arguments

Can anyone please tell me why?

Edit: I forgot to mention the following:

Upvotes: 0

Views: 49

Answers (1)

Dave S
Dave S

Reputation: 21113

From your using clause, createShapeFunction<T> is a pointer to a function which takes no arguments and returns a Shape<T>.

createShapeFunction<T>* f declares f as a pointer to a createShapeFunction<T>. Hence, f is a pointer to a pointer to a function which takes no arguments and returns a Shape<T>

Try createShapeFunction<T> function instead, which should work.


From your edit, the problem I described above isn't your issue. I find the use of a pointer to a pointer to a function to be highly suspect. I'm curious how you inserted the functions into the map.

That said, if you REALLY want this, as pointed out by François Moisan, you need to use your original definition of function and use

Shape<T> returnShape = (*function)();

Note, return shape is a Shape<T>, since that's what the function returns.

Upvotes: 1

Related Questions