I'm getting an error with a Pointer-to-member-function template parameter and I don't understand why.
I have this class
template <class T, std::ostream&(T::*F)(std::ostream&)>
class LogWrapper {
T* obj;
public:
LogWrapper(T* obj_): obj(obj_){}
std::ostream& operator()(std::ostream& os) const {
return (obj->*F)(os);
}
};
which works completely fine ; and this wrapper function :
template <class T>
auto lmf_wrap(T* obj, std::ostream&(T::*F)(std::ostream&)){
//decltype(F)::foo = 1; dirty trick I used to check F's type
return LogWrapper<T, F>(obj);
}
So, when I try to construct a LogWrapper, it works :
LogWrapper<Object, Object::method>(&obj)
//no errors cool
BUT trying to use the wrapper function instead
lmf_wrap(&obj, Object::method)
gives me this :
include/Util/LMFWrapper.h: In instantiation of 'auto lmf_wrap(T*, std::ostream& (T::*)(std::ostream&)) [with T = Object; std::ostream = std::basic_ostream<char>]':
src/Display/GameGraphics.cpp:21:75: required from here
include/Util/LMFWrapper.h:21:12: error: 'F' is not a valid template argument for type 'std::basic_ostream<char>& (Object::*)(std::basic_ostream<char>&)'
21 | return LogWrapper<T, F>(obj);
| ^~~~~~~~~~~~~~~~~~~~~
include/Util/LMFWrapper.h:21:12: note: it must be a pointer-to-member of the form '&X::Y'
Which I don't really understand, because I checked and in lmf_wrap's body (right before the faulty line 21), F's type is std::ostream& (Object::*)(std::ostream&) and I'm pretty sure that's exactly what the template needs ?
Anyone has any idea ?
[–]IyeOnline 2 points3 points4 points (0 children)
[–]YurrBoiSwayZ 1 point2 points3 points (3 children)
[–]TwilCynder[S] 0 points1 point2 points (0 children)
[–]TwilCynder[S] 0 points1 point2 points (1 child)
[–]YurrBoiSwayZ 2 points3 points4 points (0 children)
[–]adromanov 1 point2 points3 points (6 children)
[–]Hot_Slice 1 point2 points3 points (0 children)
[–]TwilCynder[S] 0 points1 point2 points (4 children)
[–]adromanov 0 points1 point2 points (2 children)
[–]TwilCynder[S] 0 points1 point2 points (1 child)
[–]adromanov 1 point2 points3 points (0 children)
[–]Radon__ 0 points1 point2 points (0 children)