Если это дубликат, я утверждаю. В моих поисках я ничего не нашел.
Я могу использовать любую из новейших функций С++ 11/С++ 14. При необходимости я могу перейти на VS2015.
Я пытаюсь написать класс, который будет автоматически добавлен в std:: function с определенной подписью при назначении. У меня есть код, который работает с GCC, но это не удалось на MSVC2013. Код - это фрагмент, который воссоздает ошибку. WTF MSVC?!
Кроме того, я знаю, что это рискованный код, автоматически набрасывая указатели на функции и т.д., но это для частной реализации библиотеки плагинов, и я только хочу определить подпись функции один раз.
Если есть другой способ написать код, который выполняет те же функции в main() и работает на обоих, я все уши.
GCC С++ 11 отлично работает - Демо
#include <functional>
#include <string>
#include <iostream>
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(std::string msg){
std::cout << "Hello " << msg << std::endl;
}
int main() {
FunctionPointer f((void*)hello);
std::function<void(std::string)> func_hello = f;
func_hello("World!");
return 0;
}
MSVC работает, когда я меняю строку на это...
std::function<void(std::string)> func_hello = f.operator std::function<void(std::string)>();
MSVC терпит неудачу с той же ошибкой, когда у меня есть это...
std::function<void(std::string)> func_hello = (std::function<void(std::string)>)f;
Ошибка MSVC со следующей ошибкой в файле, который трудно читать, если не сказать больше. Кажется, он выводит неправильную сигнатуру функции.
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> ]