假设我有以下课程:
class MyStringClass
{
public:
operator const char*() const;
};
如果可能,如何创建指向此重载强制转换运算符的函数指针?
基本上我想使用 boost::phoenix 来调用这个操作符。我假设我需要绑定到它(因此我需要创建一个指向它的函数指针),但是如果 boost::phoenix 内置了以特殊方式调用它的功能,我也对此持开放态度。
我正在使用 Visual Studio 2008,C++03。
假设我有以下课程:
class MyStringClass
{
public:
operator const char*() const;
};
如果可能,如何创建指向此重载强制转换运算符的函数指针?
基本上我想使用 boost::phoenix 来调用这个操作符。我假设我需要绑定到它(因此我需要创建一个指向它的函数指针),但是如果 boost::phoenix 内置了以特殊方式调用它的功能,我也对此持开放态度。
我正在使用 Visual Studio 2008,C++03。
const char* (MyStringClass::*ptr)() const = &MyStringClass::operator const char*;
只需使用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));
}