Re: template codegeneration issues

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Serge Skorokhodov (216716244) wrote:
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];
};

You should probably define buf_sz:

template <int sr>
const int myContainedClass::buf_sz;

It is undefined behaviour (or is it ill-formed?) to use buf_sz as anything but a integral constant expression if you don't have the definition. For example, you can't pass it to a function, or assign it to any variable.

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;)

I've not come across this, no. Without seeing a full(er) example, it's hard to say whether it is a compiler bug or your code at fault.


Tom
.



Relevant Pages

  • template codegeneration issues
    ... The template classes contain ... The values of non-static const members is changed in run-time. ... template classes seem to similarly "overlap" in memory:( ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Member Templates
    ... > class MyClass ... The book says nothing about instantiation an use of member ... > Can anybody give me an axample of use of such template ... member 'Embedded' is declared "private". ...
    (comp.lang.cpp)
  • Re: Counting how many time a template is accessed
    ... This template has been loaded onto everyone's ... >>>Public Sub AutoExec() ... >>>Private Const mcIncrementSize As Long = 10 ... >>>Private mstrCounterFile As String ...
    (microsoft.public.word.vba.general)
  • Re: Can one save a reference added to project when automating Word
    ... It does appear that if working thru the Word object, a copy of the template ... ' Private Const strReference As String = "MyReference" ... Private blnUseAppWord As Boolean ...
    (microsoft.public.word.vba.general)
  • Count Template Usage
    ... I need to count how many times each template is used. ... Dim strUserName As String, cCounterFileFullPath As String ... Private Const mcIncrementSize As Long = 10 ...
    (microsoft.public.word.vba.general)