Solved

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

Posted on 2003-12-02
5
269 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
No it didn't worked. But your work around solved the problem thanks.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now