如何自动从函数类型推断返回类型?
我正在使用 boost::python
创建 C++ 库的 Python 包装器。在某些时候,boost::python 需要一个指向成员函数(或兼容的东西)的指针,例如:
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);
由于我正在包装的类的设置器采用以下形式:
template <class MyClass, typename ValueType>
MyClass& (MyClass::*setter_method)(ValueType)
我写了一个“转换”功能:
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>);
到目前为止,效果很好,但我想知道是否有一种方法可以使其变得更加“容易”(使用)。
您是否认为有可能得到类似的结果:
// 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
来处理我的具体情况)。
谢谢。
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);
}
Which I can use like that:
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));
All solutions (even ones that explain how to specialize boost::python
to handle my specific case) are welcome.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上可以使用强制转换运算符来推断函数的返回类型 - 我对此进行了描述
这是简短的版本:
这里,编译器将推断
T
的值,从而推断func
的返回类型。It is actually possible to infer the return type of a function using casting operators -- I describe this here.
Here's the short version:
Here, the compiler will infer the value of
T
, thus the return type offunc
is also inferred.C++ 中不能自动推导返回类型,也不能根据返回类型重载函数。
在 C++0x 中,有一个名为 decltype 的新功能,您可能会感兴趣。
The return type cannot be automatically deduced in C++ and you cannot overload functions based on return type.
In C++0x there is a new feature called decltype that might be of interest.