Solved

Why is this template class changing the output of General and not OldVars?

Posted on 2003-12-02
5
298 Views
Last Modified: 2010-04-17
I am compiling the below code using Microsoft VC++ 6.0. But the setall function below is only changing General::a and not OldVars::a. Why? And what should I do so that I can change both General::a and OldVars::a?

#include <fstream.h>

class d{};
class general {};
class oldvar {};
class newvar {};

template <class T> class test
{
      public:
            static int a;
};

int test<general>::a = 321;
int test<oldvar>::a = 420;
int test<newvar>::a = 123;

typedef test<general> General;
typedef test<oldvar> OldVar;
typedef test<newvar> NewVar;

template <typename T>
void set(int a)
{
      T::a=a;
}

void setall(int x, int y)
{
      set<OldVar>(y);
      set<General>(x);
}

int main(int argc, char* argv[])
{
      cout << "General " << General::a << endl;
      cout << "OldVar " << OldVar::a << endl;
      cout << "NewVar " << NewVar::a << endl << endl;
      setall(5,10);
      cout << "General " << General::a << endl;
      cout << "OldVar " << OldVar::a << endl;
      cout << "NewVar " << NewVar::a << endl;
      return 0;
}
0
Comment
Question by:shuklasunil
  • 3
  • 2
5 Comments
 

Author Comment

by:shuklasunil
ID: 9858564
To add, strangely it looks like "set<OldVar>(y)" doesn’t calls "set<OldVar>(int)" function but calls set<General>(x). Therefore its only changing General::a.
Its still not clear why it behaving this way. I confirmed this by reversing the or of "set" function then it changes OldVar::a. :(
0
 
LVL 2

Expert Comment

by:colmcc
ID: 9878973
Interesting.  I get similar results using Borland.  Except, with Borland it's OldVar, not General, that gets changed.

I've got some idea what is going on.  I think it is to do with template instantiation.  It would appear that because the signature of the set() function does not contain T, only one instantiation is being generated.  All calls of set<Type>(int) are actually calling the same function.

I got the desired results by adding a dummy 2nd parameter -

template <typename T>
void set(int a, T* dummy)
{...}

and calling like this -
set<OldVar>(y, new OldVar);
set<General>(x, new General);

That's hardly ideal though!

It may be possible to force the compiler to generate all the desired functions.  I tried adding the following lines to force explicit instantiation -

template void set<General>(int);
template void set<Oldvar>(int);
template void set<newvar>(int);

but Borland does not seem to support this syntax.  You could try that in MSVC++.  However, it may be that you absolutely must refer to T in the function signature.

Regards,
Colin


0
 

Author Comment

by:shuklasunil
ID: 9880201
But why is it behaving like this. Is this expected/right behavior? If not does it mean there is a flaw in our compilers.
0
 
LVL 2

Accepted Solution

by:
colmcc earned 200 total points
ID: 9882024
I'm not sure.  I think it might be undefined.  I'd be interested to here if the explicit instantiation trick works for you.  The C++ standard seems to imply that it should.
0
 

Author Comment

by:shuklasunil
ID: 9888387
No it didn't worked. But your work around solved the problem thanks.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Scripting vs. Programming languages 25 175
Apps blocked by Java 9 88
Selenium and Interactive Data Language 3 42
AvlTree-Node Data type 4 8
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question