Solved

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

Posted on 2003-12-02
5
282 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
array11 challenge 16 61
wordappend challenge 8 149
PHP question(s) about order of output 9 57
detecting  the added row index in a datagridview 3 61
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This is an explanation of a simple data model to help parse a JSON feed
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

895 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