c++ template parameters

0be69bde92e8c5bfb313537ab9d4fe76
0
ElOmmy 101 Oct 03, 2005 at 23:50

Hi!
first post here ;)

what kind of types are allowed as template parameters in c++?
what i need is a template class taking a known pointer-to-classmember as a compile-time parameter, like:

typedef int MyClass::* member_pointer;

template <member_pointer pm>
class NotWorking
{
void func(MyClass* pClass)
{
pClass->*pm = 1;
}
};

void test
{
NotWorking< &MyClass::var1 > compile_error;
}

This doesn’t work ( VS2003.NET )…
Still c++ *is* able to have pointers, whose addresses are known at compile-time, as template parameters. Obviously &MyClass::var1 should be known at compile-time… so is this a VS2003.NET bug? or is there a nice workaround? or what am i doing wrong?

Thx
ElOmmy

5 Replies

Please log in or register to post a reply.

354419c232983843ec4434f002922f00
0
Altair 101 Oct 04, 2005 at 01:27

It’s working in .net 2003 but you just need to use correct c++ syntax. Change your typedef to:

typedef int(MyClass::*member_pointer)();

and method call to:

(pClass->*pm)();

And it’ll work.

Templates can take types (typename/class), integral values (enums/ints/chars/etc.) and pointers (object and free/member function) as arguments. Some compilers also take floats/doubles as template argument, but that’s not allowed by the c++ standard.

Cheers, Altair

0be69bde92e8c5bfb313537ab9d4fe76
0
ElOmmy 101 Oct 04, 2005 at 11:22

Thanks for the reply!

What you mean is a pointer-to-memberfunction.
I was using a pointer-to-membervariable (or whatever you call it):

struct Test
{
int x;
};

void func()
{
Test* p = new Test;
int Test::* pmX = &Test::x;
// now use member pointer to assign new value to x
p->*pmX = 2;
}

anyways, i just tested using pointer-to-memberfunction as a template parameter as you suggested - and it works great. I just can’t see how this behaviour is supposed to be consistent: allowing pointers to variables, pointers to member functions but not pointers to member variables.

ElOmmy

25bbd22b0b17f557748f601922880554
0
bramz 101 Oct 04, 2005 at 11:33

AFAIK pointer to member variables are allowed by the standard as well … (I guess it just slipped out of Altair’s list ;) ). But you _must_ declare MyClass before doing the typedef! And when using the typedef, the compiler also must known var1 is a member of MyClass. Thus add this in front of your code:

struct MyClass
{
    int var1;
};

I tested it and it works fine (msvc2003)

Also, you should add () to void test :)

Bramz

340bf64ac6abda6e40f7e860279823cb
0
_oisyn 101 Oct 04, 2005 at 16:06

Indeed, your syntax is correct, if you add the () and put the definition of MyClass before your code, it compiles fine.

Also, next time include some compile errors in your post, that gives the experts more of a clue what exactly is going on :)

354419c232983843ec4434f002922f00
0
Altair 101 Oct 04, 2005 at 16:30

@ElOmmy

Thanks for the reply! What you mean is a pointer-to-memberfunction.
I was using a pointer-to-membervariable (or whatever you call it):

Ah, right. It also works fine in .net 2003 if you make the member variable pointer a template argument.

Cheers, Altair