Re: template codegeneration issues
- From: "Tom Widmer [VC++ MVP]" <tom_usenet@xxxxxxxxxxx>
- Date: Fri, 04 Nov 2005 10:51:42 +0000
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 .
- Follow-Ups:
- Re: template codegeneration issues
- From: Serge Skorokhodov (216716244)
- Re: template codegeneration issues
- References:
- template codegeneration issues
- From: Serge Skorokhodov (216716244)
- template codegeneration issues
- Prev by Date: template codegeneration issues
- Next by Date: Re: template codegeneration issues
- Previous by thread: template codegeneration issues
- Next by thread: Re: template codegeneration issues
- Index(es):
Relevant Pages
|