假设我有以下课程:
class MyStringClass
{
public:
operator const char*() const;
};
如果可能,如何创建指向此重载的转换操作符的函数指针?
基本上我想用boost :: phoenix来调用这个运算符。我假设我需要绑定它(因此我需要创建一个指向它的函数指针),但是如果boost :: phoenix内置了以特殊方式调用它的功能,我也会对它开放。< / p>
我正在使用Visual Studio 2008,C ++ 03。
答案 0 :(得分:2)
const char* (MyStringClass::*ptr)() const = &MyStringClass::operator const char*;
答案 1 :(得分:1)
只需使用phx::static_cast_
: Live On Coliru
int main()
{
auto implicit_conversion = phx::static_cast_<const char*>(arg1);
std::vector<MyStringClass> v(10);
std::for_each(v.begin(), v.end(), implicit_conversion);
}
或者包裹一个仿函数: Live On Coliru
namespace detail
{
template <typename T>
struct my_cast
{
template <typename U> struct result { typedef T type; };
template <typename U>
T operator()(U v) const {
return static_cast<T>(v);
}
};
}
namespace phx = boost::phoenix;
using namespace phx::arg_names;
int main()
{
phx::function<detail::my_cast<const char*>> to_csz;
std::vector<MyStringClass> v(10);
std::for_each(v.begin(), v.end(), to_csz(arg1));
}