problem with template

Started by
1 comment, last by Eddycharly 18 years ago
Hi. I just spent one hour hunting a very stupid bug in my code. The problem was that a template parameter and a structure in my code had the same name. The compiler didn't say anything but it was making a confusion. I can change the name but it's not very elegant, the more i had new structures in my code, the more i risk to face the problem again. Here what was the problematic code : I have a class wrapper class that encapsulates D3D, one of the methods in it is : template <typename VERTEX> bool DrawTriangles (const VERTEX * const verts, const unsigned int num_verts) { if (d3d9_device) { d3d9_device->BeginScene (); d3d9_device->SetFVF (VERTEX::VERTEX_FORMAT); d3d9_device->DrawPrimitiveUP (D3DPT_TRIANGLELIST, num_verts / 3, verts, sizeof (VERTEX)); d3d9_device->EndScene (); } return true; } And somewhere else, i have a structure named VERTEX. I also have a structure named QUAD_VERTEX i use for rendering transformed vertices. Now, when i was sending vertices of the type QUAD_VERTEX to the DrawTriangles method, instead of substituting the template parameter named VERTEX to the QUAD_VERTEX type, and then use QUAD_VERTEX::VERTEX_FORMAT to set the FVF, it was using VERTEX::VERTEX_FORMAT. Of course VERTEX::VERTEX_FORMAT exists because of the VERTEX structure, but it's not at all what i wanted to code. When i wrote d3d9_device->SetFVF (VERTEX::VERTEX_FORMAT); i wanted VERTEX to refer to the template parameter. I find it very strange that the preference is not given to the template parameter, and at least my compiler should warn me about a possible conflict. Is this a bug in the compiler ? Is there something i can do in my code to prevent this to happen again ? I hope someone can help me here. I would really apreciate it. Thanks in advance.
Advertisement
struct Test1{   static const int val = 1;};struct Test2{   static const int val = 2;};template<typename Test1>void test_mbox(Test1 * pt){   if (Test1::val == 1)      MessageBox(NULL, "1", "test_mbox", MB_OK);   else if (Test1::val == 2)      MessageBox(NULL, "2", "test_mbox", MB_OK);   else      MessageBox(NULL, "undefined", "test_mbox", MB_OK);};// calling with:Test2 t2;test_mbox(&t2);


Output (on VS 2003):
2

So the substitution was done correctly.
What compiler are you using?
Thanks for making the test.

I use VC6, i know it's a very bad compiler regarding templates support, but i thought it could handle such a simple case correctly.
There's nothing fancy here, no specialization, etc...

I took your code and ran it, and guess what? my output is 1...
Then i replaced the template parameter name by TestXXX, and the ouput is 2.

I suppose it's time to upgrade to a more recent version.
Thanks.

PS: is it legal to initialize a static structure member directly in the structure declaration ? i thought no, and VC6 complained about that. i had to use enums instead.

Once again, thanks.

This topic is closed to new replies.

Advertisement