typecast for member function pointer

I

icedac

<code>
struct nullptr_t
{
nullptr_t() : ptr_(0) {}

// pointer
template < typename T >
operator T*() const
{ return (T*)0; }

// function pointer
template < typename R >
struct typedef_fun_ptr
{
typedef R (*type)();
};
template < typename R >
operator typename typedef_fun_ptr<R>::type () const
{
return (typedef_fun_ptr<R>::type)0;
}
// arg1 ... arg10

// for member function pointer (doesn't works)
template < typename C, typename R >
struct typedef_memfun_ptr
{
typedef R (C::*type)();
};
template < typename C, typename R >
operator typename typedef_memfun_ptr<C,R>::type () const
{
return (typedef_memfun_ptr<C,R>::type)0;
}
// arg1 ... arg10

private:
void* ptr_;
};
const nullptr_t nullptr;

</code>

this is my code for nullptr in C++98.
it works for normal pointers, function pointers but not for member
pointers.
anybody knows why it doesn't works?

sample code is follows
<code>
int* p = nullptr;
std::string* s = nullptr;
void* vp = nullptr;
assert( p == nullptr );
assert( s == nullptr );
assert( sizeof(nullptr) == sizeof(void*) );

// int i = nullptr; // compile-time error: ok

// function pointer
int (*fptr1)() = nullptr;
void* (*fptr2)() = nullptr;


int (nullptr_test::*memfunp2)() =
nullptr.get_nullptr<nullptr_test,int>();
int (nullptr_test::*memfunp3)() = nullptr; // don't works
</code>

the error message is
<error_msg>
1>d:\_work\qoom-sr\qoomserver\trunk\worldserver\asioserver
\win_main.cpp(162) : error C2440: 'initializing' : couln't convert
from 'const nullptr_t' to 'int (__thiscall nullptr_test::* )(void)'
</error_msg>
 
I

icedac

thanx for the url!
that code works perfect!
now i realized that my complicated code for fun_ptr didn't works for
fun_ptr :0
 
J

James Kanze

Just a nit, but there is a very, very widespread convention
(from the C standard) that names ending in _t are typedefs.
(Not that it really matters here, since the user is not supposed
to ever use this name anyway.)
Just by comparing a cleaner implementation by Scott Meyerhttp://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/nullptr

This solution is not without drawbacks. In particular, it means
that not only the null pointer constant is not a pointer, but
it's not a constant either, at least as far as the compiler is
concerned. It also introduces an additional user defined
conversion, which affects overload resolution (although I'm not
convinced that this is a bad thing).

Anyway, you might want to see
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2214.pdf.
If I'm not mistaken, this has been adopted for inclusion in the
next version of the standard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top