Syntax for Pointer-to-member-VARIABLE?

S

s5n

Hello, all!

i KNOW i saw a reference to this in a book once, labeled as a "rarely
used" feature (i think it was Scott Meyers), but i can't for the life
of me find an example of it now (i even dug through Stroustrup's
book).

a) What is the syntax for a pointer-to-member-variable (NOT member
function)
b) Can such a pointer be used as a template parameter argument, like a
function pointer can?

i'd be very grateful for any tips!
 
S

Stephan Beal

Basically the same, just omit the arguments.
     void (A::*pFun)(int);
     int A::*pData;

Great! Thanks!
Mmm...  I am not sure.  Probably yes.  I think I've never needed that.

i had never needed it until now, either. :) i'll give it a try and see
if it does the trick.

(i'm using member function pointers to map C++ functions to their
JavaScript-side counterparts using the Google v8 JS engine. Now i'd
like to try binding access to data members to JS.)
 
S

Stephan Beal

i had never needed it until now, either. :) i'll give it a try and see
if it does the trick.

(i'm using member function pointers to map C++ functions to their
JavaScript-side counterparts using the Google v8 JS engine. Now i'd
like to try binding access to data members to JS.)

The answer to (b) appears to be yes! Woohoo! That just saved me tons
of work...

template <typename WrappedType, typename PropertyType, PropertyType
WrappedType::*MemVar>
Handle<Value> GetMemVar(Local<String> property,
const AccessorInfo &info)
{
WrappedType * self
= ::v8::juice::WeakJSClassCreator<WrappedType>::GetSelf( info.This
() );
if( ! self ) return v8::ThrowException( v8::String::New( "Getter
function could not access native object!" ) );
return convert::CastToJS( (self->*MemVar) );
}

template <typename WrappedType, typename PropertyType, PropertyType
WrappedType::*MemVar>
void SetMemVar(Local<String> property, Local<Value> value,
const AccessorInfo& info)
{
WrappedType * self
= ::v8::juice::WeakJSClassCreator<WrappedType>::GetSelf( info.This
() );
if( self )
{
self->*MemVar = convert::CastFromJS<PropertyType>( value );
}
else
{
CERR << "Setter function could not access native object!\n";
}
}
 
M

Michael Mol

Hello, all!

i KNOW i saw a reference to this in a book once, labeled as a "rarely
used" feature (i think it was Scott Meyers), but i can't for the life
of me find an example of it now (i even dug through Stroustrup's
book).

We use it extensively where I work.
b) Can such a pointer be used as a template parameter argument, like a
function pointer can?

I don't know. Without knowledge of the function's parameters before-
hand, you may have a hard time actually using it in the template
code. Instead, I use two types as a template arguments...one for the
class's type, and another for a struct I use to represent the argument
list. I then generate the pointer-to-member type from those. (I'm not
templatizing the return type. Haven't had a need to, yet.)
 
S

Stephan Beal

We use it extensively where I work.


I don't know.  Without knowledge of the function's parameters before-
hand, you may have a hard time actually using it in the template
code.

In my particular code the funcs are forwarding to a standard interface
of the JavaScript engine, so the signatures all follow a well-known
pattern.
 Instead, I use two types as a template arguments...one for the
class's type, and another for a struct I use to represent the argument
list.

That's an idea. i'll have to think about whether that might help me
reduce some code (all of my generated overloads for N arguments).
 I then generate the pointer-to-member type from those. (I'm not
templatizing the return type.  Haven't had a need to, yet.)

In my case i've got to "cast" the parameter type to/from JS/C++, so
i've got to have the type to give to the conversion operators
(functors which use ParameterType-specific specializations to drive
the conversion).

i'm so happy this works! A very useful feature in under 10 minutes!

:-D
 
M

Michael Mol

In my case i've got to "cast" the parameter type to/from JS/C++, so
i've got to have the type to give to the conversion operators
(functors which use ParameterType-specific specializations to drive
the conversion).

i'm so happy this works! A very useful feature in under 10 minutes!

Just be very, very careful. Not all ptr-to-member-funcs are the same
size. And remember that virtual function rules still apply.
 
S

Stephan Beal

Just be very, very careful.  Not all ptr-to-member-funcs are the same
size.  And remember that virtual function rules still apply.

The funcs are specified as template parameters, as opposed to function
parameters, e.g.:

template <typename T, typename ReturnT, ReturnT (T::*Func)()>
struct Func0 {...operator to forward from JS call to Func()... };
// repeated for up to N arguments, including specializations for const
members.

and then used (indirectly, via another layer of templates) like this:

typedef v8::Handle< v8::Value > HV;
typedef StatementWrapper STW;
stmt.BindMemFunc< bool, &STW::isPrepared >("isPrepared");
stmt.BindMemFunc< HV, int, &STW::bindNull >( "bindNull" );
stmt.BindMemFunc< HV, int, int, &STW::bindNum<int> >
( "bindInt" );
stmt.BindMemFunc< HV, int, double, &STW::bindNum<double> >
( "bindDouble" );

so the function references are baked in at compile time.
Virtualness... that's a point i hadn't considered. Haven't tested it,
either, now that i think about it. Doh...
 

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

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,431
Latest member
ElyseG3173

Latest Threads

Top