Re: instantiate templates by template class<args>; fails without /Zi ! ??
On Mon, 16 Jun 2008 11:56:29 +0200, "mario semo" <mario_semo@hotmail.com>
wrote:
Hello,
In the following sample a template is instantiated by "template
class<type>".
This should create a static class member and initialize it by calling
getNum().
This operates as expected only when /Zi is used. (independent of /O compile
settings! )
When i compile
cl Prob54.cpp
Prob54
-- no output
cl /Zi Prob54.cpp
Prob54
-- output : 124
cl /Zi /O2 Prob54.cpp
Prob54
-- output : 124
Here is the sample source:
==== Prob54.cpp ===========================
#include <stdio.h>
int getNum()
{
static int i=123;
i++;
printf("%d\n",i);
fflush(0);
return i;
}
template <class Foo>
class Bar
{
public:
Bar(Foo pFoo)
: mFoo(pFoo)
{
}
virtual ~Bar()
{
}
static int sFoo;
Foo mFoo;
};
template <class Foo>
int Bar<Foo>::sFoo = getNum();
template Bar<int>;
Should be "template class Bar<int>;"
int main
(
int argc
,char *argv[]
)
{
return 0;
}
======= EOF ============
Whats the reason for this behaviour?
This is even the case, when my getNum function is placed in another .cpp
file and referenced via
extern int getNum();
Note : My real code is registering exemplars for make OBject functionalities
in a similar way. There is no instance of the template, except the
one required to create the static data item which calls another function to
register the examplar constructor....
You are explicitly instantiating Bar<int>, and 14.7.2/7 says this will
instantiate all its members:
<q>
The explicit instantiation of a class template specialization implies the
instantiation of all of its members
not previously explicitly specialized in the translation unit containing
the explicit instantiation.
</q>
So this is a bug, which you can report here:
https://connect.microsoft.com/feedback/default.aspx?SiteID=210
For a workaround, try adding this to the Bar ctor:
memmove(&sFoo, &sFoo, sizeof(sFoo));
I encountered a similar bug in VC7, and this fixed it; here's the comment I
made at the time:
// NB: The memmove is a workaround for overly aggressive compilers
// like VC7.0 which otherwise optimize the sdata object out of
// existence. Per 14.7.1, this has to be considered a compiler bug,
// because by passing sdata to StaticDataInstance, callers "use"
// sdata in a way that requires it to exist. Note that VC6 and
// VC.NET 2003 don't exhibit this bug.
I'll have to run the code through VC9 to see if it's reappeared.
--
Doug Harrison
Visual C++ MVP