Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用boost::variant指向成员函数的指针_C++_Pointers_Boost_Casting - Fatal编程技术网

C++ 使用boost::variant指向成员函数的指针

C++ 使用boost::variant指向成员函数的指针,c++,pointers,boost,casting,C++,Pointers,Boost,Casting,我试图定义一个指向成员函数的“通用”指针,该指针可用于访问特定类中的各种成员函数,如下所示: class Security{ inline std::vector<double> member_function(const std::string &input_data_string){ return ... a vector<double>....;}; }; 这是我得到的错误: cannot convert from 'std::vector

我试图定义一个指向成员函数的“通用”指针,该指针可用于访问特定类中的各种成员函数,如下所示:

class Security{
    inline std::vector<double> member_function(const std::string &input_data_string){ return ... a vector<double>....;};
    };
这是我得到的错误:

cannot convert from 'std::vector<_Ty> (__thiscall Security::* )(const std::string &)' to 'boost::variant<T0_,T1,T2,T3> (__thiscall Security::* )(boost::variant<T0_,T1,T2,T3>)'
1>          with
1>          [
1>              _Ty=double
1>          ]
1>          and
1>          [
1>              T0_=std::string,
1>              T1=double,
1>              T2=std::vector<double>,
1>              T3=std::vector<std::string>
1>          ]
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
无法从'std::vector(uu thiscall Security::*)(const std::string&')转换为'boost::variant(uu thiscall Security::*)(boost::variant)'
1> 与
1>          [
1> _Ty=double
1>          ]
1> 及
1>          [
1> T0=标准::字符串,
1> T1=双倍,
1> T2=标准::向量,
1> T3=std::vector
1>          ]
1> 指向的类型是不相关的;转换需要重新解释转换、C样式转换或函数样式转换

你能帮我找出我做错了什么吗?非常感谢。

这样不行。必须有人进行从向量到变量类型的转换,而您的函数无法进行转换(因为它返回一个向量,这就是它所知道的),创建指针的代码也无法进行转换(因为它无法动态创建函数)


另一方面,您可能希望重新思考您的设计。为什么您需要这样一个通用函数指针呢?能否更具体地加以规定?一个好的设计是通过清晰的类型来表现的,因为类型比任何文档都能更准确地向程序员显示所需的数据。

安全::成员函数的类型是
std::vector(\u thiscall Security::*)(const std::string&)
这与您试图分配给它的指针类型完全不同。这是不允许的。只能将接受并返回该变量的成员函数分配给该类型的成员函数指针

如果这个赋值有效,那么试图调用成员函数指针的代码将向函数传递一个变量,函数将被中断,因为它需要一个字符串。任何地方都没有足够的信息让任何人知道有人需要以某种方式从您的变体中获取字符串。即使所有这些都有效,当函数返回一个向量时,有人需要知道这个向量需要用来构造一个变量。这必须在调用站点完成,但调用站点甚至不知道正在返回向量。仅仅知道返回的是什么类型是不够的,编译器必须知道它。您知道该类型这一事实表明,您首先不应该使用catch-all类型。你应该使用正确的类型

如果您有一组重载函数,它们接受并返回不同的类型,那么您将无法拥有一个可以指向这些重载中任何一个的成员函数指针。试图做到这一点完全忽略了C++中的类型安全性。因此,您必须想出一种类型安全的方法来做您想做的事情


我们需要更多关于你的目标是什么的信息来帮助你找到一种方法,但是您当前尝试的是永远不会起作用的。

您是否考虑过使用
boost::function
std::function
来代替它?另外请看
boost::bind
boost::phoenix::bind
我这样做是因为我不想每次调用不同的memeber函数时都重新定义指针虽然每次我都会调用这个函数,但我知道它的返回类型。如果要通过指针调用函数,如:ptr_sec_fn=&(boost::getSecurity::member_function)…,如何进行转换。。不确定这是否是有效语法!
    ptr_sec_fn=&Security::member_function;
cannot convert from 'std::vector<_Ty> (__thiscall Security::* )(const std::string &)' to 'boost::variant<T0_,T1,T2,T3> (__thiscall Security::* )(boost::variant<T0_,T1,T2,T3>)'
1>          with
1>          [
1>              _Ty=double
1>          ]
1>          and
1>          [
1>              T0_=std::string,
1>              T1=double,
1>              T2=std::vector<double>,
1>              T3=std::vector<std::string>
1>          ]
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast