Domanda

Suppose I have the following class:

class MyStringClass
{
public:
    operator const char*() const;
};

If possible, how do I create a function pointer to this overloaded casting operator?

Basically I'd like to use boost::phoenix to invoke this operator. I'm assuming I need to bind to it (hence why I need to create a function pointer to it), but if boost::phoenix has built in functionality to invoke this in a special way I'm open to that too.

I'm using Visual Studio 2008, C++03.

È stato utile?

Soluzione

const char* (MyStringClass::*ptr)() const = &MyStringClass::operator const char*;

Altri suggerimenti

Just use 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);
}

Or wrap in a functor: 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));
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top