Jump to content


c++ template parameters


5 replies to this topic

#1 ElOmmy

    New Member

  • Members
  • Pip
  • 7 posts

Posted 03 October 2005 - 11:50 PM

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

#2 Altair

    Valued Member

  • Members
  • PipPipPip
  • 151 posts

Posted 04 October 2005 - 01:27 AM

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
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein

#3 ElOmmy

    New Member

  • Members
  • Pip
  • 7 posts

Posted 04 October 2005 - 11:22 AM

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

#4 bramz

    Valued Member

  • Members
  • PipPipPip
  • 189 posts

Posted 04 October 2005 - 11:33 AM

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
hi, i'm a signature viruz, plz set me as your signature and help me spread :)
Bramz' warehouse | LiAR isn't a raytracer

#5 .oisyn

    DevMaster Staff

  • Moderators
  • 1842 posts

Posted 04 October 2005 - 04:06 PM

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 :)
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#6 Altair

    Valued Member

  • Members
  • PipPipPip
  • 151 posts

Posted 04 October 2005 - 04:30 PM

ElOmmy said:

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
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users