If this is a duplicate I appologize. I couldn't find anything in my searches though.
I can use any of the newest features c++11/c++14. I can upgrade to VS2015 if necessary.
I'm trying to write a class that will auto cast into a std::function with a specific signature when assigned. I have code that works with GCC, but it failed on MSVC2013. The code is a snippet that recreates the error. WTF MSVC?!
Also I know this is risky code, automatically casting function pointers and such, but it's for the private implementation of a plugin library and I only want to define the function signature once.
GCC c++11 works fine - Demo
class FunctionPointer
{
void* fp;
public:
FunctionPointer(void* ptr)
: fp(ptr)
{}
// Overload casting operator to
// a certain function signiture
template<class R, class... ARGS>
operator std::function<R(ARGS...)>(){
typedef R(*func_ptr)(ARGS...);
return std::function<R(ARGS...)>((func_ptr)fp);
}
};
void hello(string msg){
cout << "Hello " << msg << endl;
}
int main() {
FunctionPointer f((void*)hello);
function<void(string)> func_hello = f;
func_hello("World!");
return 0;
}
MSVC works when I change the line to this...
function<void(string)> func_hello = f.operator std::function<void(string)>();
MSVC fails with the same error when I have this...
function<void(string)> func_hello = (std::function<void(string)>)f;
MSVC fails with the following error in a file that is hard to read to say the least. It seems to be deducing the wrong function signature.
xrefwrap.h:283 - error C2064: term does not evaluate to a function taking 1 arguments
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xrefwrap(283): error C2064: term does not evaluate to a function taking 1 arguments
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(228) : see reference to function template instantiation '_Ret std::_Callable_obj<FunctionPointer,false>::_ApplyX<_Rx,_Ty>(_Ty &&)' being compiled
1> with
1> [
1> _Ret=void
1> , _Rx=void
1> , _Ty=std::basic_string<char,std::char_traits<char>,std::allocator<char>>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(228) : see reference to function template instantiation '_Ret std::_Callable_obj<FunctionPointer,false>::_ApplyX<_Rx,_Ty>(_Ty &&)' being compiled
1> with
1> [
1> _Ret=void
1> , _Rx=void
1> , _Ty=std::basic_string<char,std::char_traits<char>,std::allocator<char>>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(226) : while compiling class template member function 'void std::_Func_impl<_MyWrapper,_Alloc,_Ret,std::string>::_Do_call(std::string &&)'
1> with
1> [
1> _Alloc=std::allocator<std::_Func_class<void,std::string>>
1> , _Ret=void
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(495) : see reference to class template instantiation 'std::_Func_impl<_MyWrapper,_Alloc,_Ret,std::string>' being compiled
1> with
1> [
1> _Alloc=std::allocator<std::_Func_class<void,std::string>>
1> , _Ret=void
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(396) : see reference to function template instantiation 'void std::_Func_class<_Ret,std::string>::_Do_alloc<_Myimpl,FunctionPointer&,_Alloc>(_Fty,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Alloc=std::allocator<std::_Func_class<void,std::string>>
1> , _Fty=FunctionPointer &
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(396) : see reference to function template instantiation 'void std::_Func_class<_Ret,std::string>::_Do_alloc<_Myimpl,FunctionPointer&,_Alloc>(_Fty,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Alloc=std::allocator<std::_Func_class<void,std::string>>
1> , _Fty=FunctionPointer &
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(385) : see reference to function template instantiation 'void std::_Func_class<_Ret,std::string>::_Reset_alloc<FunctionPointer&,std::allocator<std::_Func_class<_Ret,std::string>>>(_Fty,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Fty=FunctionPointer &
1> , _Alloc=std::allocator<std::_Func_class<void,std::string>>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(385) : see reference to function template instantiation 'void std::_Func_class<_Ret,std::string>::_Reset_alloc<FunctionPointer&,std::allocator<std::_Func_class<_Ret,std::string>>>(_Fty,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Fty=FunctionPointer &
1> , _Alloc=std::allocator<std::_Func_class<void,std::string>>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(671) : see reference to function template instantiation 'void std::_Func_class<_Ret,std::string>::_Reset<FunctionPointer&>(_Fty)' being compiled
1> with
1> [
1> _Ret=void
1> , _Fty=FunctionPointer &
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(671) : see reference to function template instantiation 'void std::_Func_class<_Ret,std::string>::_Reset<FunctionPointer&>(_Fty)' being compiled
1> with
1> [
1> _Ret=void
1> , _Fty=FunctionPointer &
1> ]
1> c:\users\cameron\desktop\desktop\programming\projects\c++ projects\garbage\templatetest\main.cpp(32) : see reference to function template instantiation 'std::function<void (std::string)>::function<FunctionPointer&>(_Fx)' being compiled
1> with
1> [
1> _Fx=FunctionPointer &
1> ]
1> c:\users\cameron\desktop\desktop\programming\projects\c++ projects\garbage\templatetest\main.cpp(32) : see reference to function template instantiation 'std::function<void (std::string)>::function<FunctionPointer&>(_Fx)' being compiled
1> with
1> [
1> _Fx=FunctionPointer &
1> ]
Aucun commentaire:
Enregistrer un commentaire