 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Thyfate Guest
|
Posted: Thu Jun 23, 2005 9:33 am Post subject: Static variable in template function across compilation unit |
|
|
Hello !
I am trying to write several counters for a program depending on some
types. I thus have written a template function containing a static int
which is increased each time the function is called. Across the
compilation units, the function should be instantiated only one for
each dependent type and the static variables be shared across
compilation units for each type. However under Visual C++ this is the
case when compiling in debug mode but not in release mode... Could
someone please tell me what should be the expected behavior ? and a
workaround ?
Thanks in advance,
Here is a simple test case :
// StaticInit.cpp
#include "Unit1.h"
#include "Unit2.h"
int main(int argc, char* argv[])
{
print1();
print2();
print1();
print2();
return 0;
}
// MyFunc.h
template <class T>
int myfunc(void)
{
static int i = -1;
return i++;
}
//Unit1.h
void print1();
//Unit2.h
void print2();
//Unit1.cpp
#include <iostream>
#include "Unit1.h"
#include "MyFunc.h"
void print1()
{
std::cout << "Print1 : " << myfunc
}
//Unit2.cpp
#include
#include "Unit2.h"
#include "MyFunc.h"
void print2()
{
std::cout << "Print2 : " << myfunc
}
In debug mode, the result is as expected:
Print1 : -1
Print2 : 0
Print1 : 1
Print2 : 2
However in release mode, the result is :
Print1 : -1
Print2 : -1
Print1 : 0
Print2 : 0
Thanks in advance for your help,
Marc
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Graeme Prentice Guest
|
Posted: Fri Jun 24, 2005 3:34 pm Post subject: Re: Static variable in template function across compilation |
|
|
On 23 Jun 2005 05:33:44 -0400, Thyfate wrote:
| Quote: | Hello !
I am trying to write several counters for a program depending on some
types. I thus have written a template function containing a static int
which is increased each time the function is called. Across the
compilation units, the function should be instantiated only one for
each dependent type and the static variables be shared across
compilation units for each type. However under Visual C++ this is the
case when compiling in debug mode but not in release mode... Could
someone please tell me what should be the expected behavior ? and a
workaround ?
In debug mode, the result is as expected:
Print1 : -1
Print2 : 0
Print1 : 1
Print2 : 2
However in release mode, the result is :
Print1 : -1
Print2 : -1
Print1 : 0
Print2 : 0
|
The expected behaviour is what you show -1 0 1 2. This is a fairly bad
VC bug. You could try turning off some optimisations in release mode
but the most reliable fix is to move the definition of the template
function from the header file into a cpp file and explicitly instantiate
it in that cpp file. You need a declaration (rather than a definition)
left in the header file
// header file
template <class T>
int myfunc(void);
// cpp file - one translation unit only
template <class T>
int myfunc()
{
static int i = -1;
return i++;
}
// explicit instantiation
template int myfunc<int>();
template int myfunc<char>();
....
template int myfunc<whatever>();
The linker will complain if you don't provide the right explicit
instantiations. Unfortunately this means having all the types required
for the explicit instantiations in header files, however they can be
incomplete types in the header file. You might want to contact the
Microsoft VC compiler people and see if they have fixed this bug in the
upcoming release of VC++ (or ask Herb Sutter!).
Graeme
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|