template codegeneration issues

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi,

I just seeking advice.

Some background information first because I've run into issues
that seems pretty obscure to me:( Quick search through KB yields
next to nothing. Simplified samples work OK as well, the problems
appear in rather bulky code:(

I'm trying to implement some signal processing algorithm using
templates with integral template arguments (sampling rate, window
and delay sizes etc.) in VC 7.1. The template classes contain
static and non-static const members that depend on template
arguments. Some of these classes are used as template arguments
in higher level classes.

The problems:

1. The values of non-static const members is changed in run-time.
It can be easily observed in the debbuger and it looks like
certain member variables and const members are placed at the same
memory locations. A simplified illustration may be like this:

template < int sr >
myContainedClass
{
public:
myContainedClass() { /* some code */ }
void reset_internals() { /* some code */ }

private
static const int buf_sz = int(sr*80.0/1000.0 + 0.5);
int buffer[buf_sz];
};

template < class contained_class_t, int sr >
class myClass
{
public:
myClass() : contained_class(sr), some_const(0.5)
{
// some_const == 0.5 here
contained_class.reset_internals();
// some_const == some other value here
}
private:
contained_class_t contained_class;
const double some_const;
};

2. The members of different variables of the abovementioned
template classes seem to similarly "overlap" in memory:(

SomeClass var1( /*some initialization args*/);
myClass<500> var2;
// here some var1 data is corrupted by the code of var2 ctor:(

Adding additional "dummy" data members may eliminate the errors.

Is it a known problem? Can somebody make a suggestion how to
avoid it? Maybe a workaround? I understand that using other
(simpler:) coding style will help. But I really enjoy both
flexibility and performance that C++ templates promise;)

Sorry for not messy posting but I really failed to demonstrate a
problem in a simple and short code snippet:(

TIA

--
Serge
.



Relevant Pages

  • Re: template codegeneration issues
    ... The template classes contain ... It can be easily observed in the debbuger and it looks like certain member variables and const members are placed at the same ...
    (microsoft.public.dotnet.languages.vc)
  • Re: OK in Visual C++6.0 but not in Visual C++ .NET 2003?
    ... The example you showed declared function template to be a friend of a class ... class MyClass ... This would result in InitClass being a friend of MyClassin cases where T ... You have to declare your friend a little differently ...
    (microsoft.public.dotnet.languages.vc)
  • Re: what to do with template classes in template unsupported compiler ?
    ... (uses many different kinds of each template ... rewrite the template classes such that there'll be a unique class ... write a template supported compiler for the specific platform ... current requirements new unsurmountable requirements will pop up. ...
    (comp.lang.cpp)
  • template and container, looking for advice
    ... I'm having some problems inserting some template classes into containers. ... and make sure that template parameter T is derived from BaseT but it is not ...
    (comp.lang.cpp)
  • Message map for class with a template defined base class
    ... I'm trying to write a message map for a class with a template defined base class. ... class MyClass: public BASE_TYPE ...
    (microsoft.public.vc.mfc)