如何从函数类型自动推断返回类型? [英] How to automatically infer the return type from a function type?
问题描述
我正在使用boost::python
创建C ++库的Python包装器.在某些时候,boost::python
需要一个指向成员函数(或兼容的函数)的指针,例如:
I'm using boost::python
to create a Python wrapper of a C++ library. At some point, boost::python
needs a pointer to a member function (or something compatible), like:
template <class MyClass, typename ValueType>
void (*setter_function)(MyClass&, ValueType)
// This doesn't compile, but you got the idea.
my_boost_python_call(setter_function f);
由于我要包装的类具有以下形式的设置器:
Since the class I'm wrapping has its setter in the following form:
template <class MyClass, typename ValueType>
MyClass& (MyClass::*setter_method)(ValueType)
我编写了一个转换"功能:
I wrote a single "conversion" function:
template <typename MyClass, typename ValueType, setter_method<MyClass, ValueType> fluent_setter>
void nonfluent_setter(MyClass& instance, ValueType value)
{
(instance.*fluent_setter)(value);
}
我可以这样使用:
class Foo
{
public:
Foo& bar(int value);
};
my_boost_python_call(nonfluent_setter<Foo, int, &Foo::bar>);
到目前为止,这种方法很好用,但是我想知道是否有一种方法可以使它更加容易"(使用).
So far this works well, but I wonder if there is a way to make this even more "easy" (to use).
您认为可以通过某种方式获得类似的东西吗?
Do you think it is somehow possible to get something like:
// return type is somehow inferred from the member function pointer
my_boost_python_call(nonfluent_setter<Foo, &Foo::bar>);
// or even a syntax similar to boost::python::make_function
my_boost_python_call(make_nonfluent_setter<Foo>(&Foo::bar));
欢迎所有解决方案(甚至包括解释如何专门化boost::python
来处理我的特定情况的解决方案).
All solutions (even ones that explain how to specialize boost::python
to handle my specific case) are welcome.
谢谢.
推荐答案
返回类型不能在C ++中自动推导,并且您不能基于返回类型重载函数.
The return type cannot be automatically deduced in C++ and you cannot overload functions based on return type.
在C ++ 0x中,有一个有趣的新功能称为 decltype .
In C++0x there is a new feature called decltype that might be of interest.
这篇关于如何从函数类型自动推断返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!