Solved

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

Posted on 2003-12-02
5
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Advice on Xojo as a development tool over VB. 4 90
iSeries email authority 6 112
Math Equation 23 149
Selenium docs api java index 3 98
This is an explanation of a simple data model to help parse a JSON feed
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
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…

752 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