I want a function like this:
template<typename C, typename T>
void foo(C &&aclass, T (C::*const memberFunc)(unsigned)) {
}
The parameters are (in words because C/C++ type syntax is mental):
- A universal reference to a class, e.g.
MyClass. - A const pointer to a member function of
MyClassthat takes an unsigned int and returnsT.
This sort of works, however if I call it with an l-value reference as the first parameter I get an error like:
candidate template ignored: deduced conflicting types for parameter 'C' ('MyClass &' vs. 'MyClass')
As far as I understand it, it is deducing C from the first and second parameters, but comes up with different deductions and gets confused.
According to this answer you can make it only do deduction on the first parameter, and somehow use the typename keyword on the second parameter. But I can't work out the syntax to do this when I do want it to deduce one of the types in the parameter (T), but not the other (C).
This answer is also helpful but they solve it by just not using references for C at all, which in that case is equally efficient, but not in mine.
Is this possible?
Aucun commentaire:
Enregistrer un commentaire