2

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.

void.pointer
  • 24,859
  • 31
  • 132
  • 243
  • Not sure how helpful this will end up being, but http://coliru.stacked-crooked.com/a/e342c1173970316d – chris Mar 17 '14 at 21:38

2 Answers2

2
const char* (MyStringClass::*ptr)() const = &MyStringClass::operator const char*;
Brian Bi
  • 111,498
  • 10
  • 176
  • 312
  • However, this can't be used with phoenix. Also, it will prevent inlining on most (if not all) compilers – sehe Mar 17 '14 at 21:41
  • OK, I see. I don't know what phoenix is. Anyway the question did ask how to create a pointer, so I answered that. It may be that this answer is not useful at all, but whatever. – Brian Bi Mar 17 '14 at 21:43
  • it's true that a pointer is less than useful for use with phoenix, so that part of the question seems confused – sehe Mar 17 '14 at 21:58
  • Even though I ended up using `phx::static_cast_` to solve my particular problem (thanks to @sehe), I marked this as the answer since it answers my primary question. I shouldn't have mentioned boost phoenix in my question as it just caused confusion, sorry about that. – void.pointer Mar 25 '14 at 15:52
1

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));
}
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Normally static_cast will not invoke overloaded casting operators. Does boost phoenix's static_cast not follow this behavioral pattern? Internally is it just a c-style cast, or does it do a static_cast internally? – void.pointer Mar 17 '14 at 21:47
  • Static cast does invoke user defined conversions, even if they are `explicit`. That's what static cast is for :) See [Regular cast vs. static_cast vs. dynamic_cast](http://stackoverflow.com/q/28002/85371). Anyways, the proof is in the examples. – sehe Mar 17 '14 at 21:50
  • I actually didn't know static_cast invoked overloaded typecast operators. I learn something new every day! Thanks this is great news. – void.pointer Mar 17 '14 at 23:40