Solved

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

Posted on 2003-12-02
5
305 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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
egit plugin on eclipse 8 95
ASP/VB email question 4 61
batch file or script 4 47
Java List 4 32
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
A short article about problems I had with the new location API and permissions in Marshmallow
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 …

713 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