C++ 通用函数指针

C++ 通用函数指针,c++,types,getter,C++,Types,Getter,有一些类具有如下方法: int getSomething1(); std::string getSomething2(); someClass getSomething3(); 有一个描述此类字段的结构,如: {"name of field", pointer to getter, std::type_info} 那么我想使用它如下: if(type == int){ field_int = (int)getter(); } else if(type == std::string){

有一些类具有如下方法:

int getSomething1();
std::string getSomething2();
someClass getSomething3();
有一个描述此类字段的结构,如:

{"name of field", pointer to getter, std::type_info}
那么我想使用它如下:

if(type == int){
   field_int = (int)getter();
}
else if(type == std::string){
   field_string = (std::string)getter();
}
etc.
如何将getter转换为

 int getSomething1();
 std::string getSomething2();
 etc.
指向某个通用函数指针,然后获取字段的正确值?

我的另一个问题很好地解决了您的问题。通过一些小的修改,您可以得到:

template<class C, class T>
T get_attribute(const C& instance, T (C::*func)() const) {
    return (instance.*func)();
}
您可以这样使用它:

Foo foo;
int value = get_attribute<Foo, int>(foo, &Foo::getSomething1);
std::string value = get_attribute<Foo, std::string>(foo,  &Foo::getSomething2);
someClass value = get_attribute<Foo, someClass>(foo,  &Foo::getSomething3);

当然,您可以将get_属性转换为函子,以绑定部分或全部参数。

您试图实现的目标可以通过现有容器(如序列)更好地实现。我建议你先试试这个。

模板救命

// Create mapping of type to specific function
template <typename T> T getSomething(); // No default implementation
template <> int getSomething<int>() { return getSomething1(); } 
template <> std::string getSomething<std::string>() { return getSomething2(); }
template <> someClass getSomething<someClass>() { return getSomething3(); }
// Convenience wrapper
template <typename T> void getSomething(T& t) { t = getSomething<T>(); }
// Use
int i = getSomething<int>();
std::string s;
getSomething(s);

没有正式的通用函数指针,相当于void* 数据。通常的解决方案是使用void*;你是有保证的 您可以将任何非成员函数指针转换为此或任何 其他函数指针类型并返回而不丢失信息

如果函数签名中存在某种相似性,例如 是getter,没有参数以及它们是如何使用的,可能是这样的 可以使用抽象基类和一组派生的 可能模板化的类;将指针放在这些实例上 地图中的类肯定比巨大的类更优雅
开关

据我所知,您的困难在于存储函数指针,因为它们的类型不同。您可以使用和解决此问题


我认为,模板元编程是解决方案!你想在编译时还是在运行时检测到这个问题?我觉得比这个问题要复杂一点-从这个问题上看,OP持有一个二级结构,其中应该存储这些指向某个东西的指针。@Nim:是的,我刚刚知道。我已经相应地修改了它,它应该可以正常工作。@Nim这里真正的问题是,他想动态地选择函数。无论函数本身是否是模板,在这方面都不会真正改变任何东西。如前所述,模板看起来像是多余的废话;除了增加代码行数之外,它似乎没有做任何具体的事情。除了额外的代码行,模板给我们带来了什么?如果您想将调用封装在某种类中,我可以看到如何使用模板。但我需要更多地了解他的实际用途,才能说出在这种情况下什么可能是合适的。@Bjorn,thx-我通常只使用最新版本-但这种链接更好。。。
// Create mapping of type to specific function
template <typename T> T getSomething(); // No default implementation
template <> int getSomething<int>() { return getSomething1(); } 
template <> std::string getSomething<std::string>() { return getSomething2(); }
template <> someClass getSomething<someClass>() { return getSomething3(); }
// Convenience wrapper
template <typename T> void getSomething(T& t) { t = getSomething<T>(); }
// Use
int i = getSomething<int>();
std::string s;
getSomething(s);
#include <boost/any.hpp>
#include <boost/function.hpp>

int getInt() {
    return 0;  
}

std::string getString() {
    return "hello";
}

int main()
{
    boost::function<boost::any ()> intFunc(getInt);
    boost::function<boost::any ()> strFunc(getString);

    int i = boost::any_cast<int>(intFunc());
    std::string str = boost::any_cast<std::string>(strFunc());
}