C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template Parameter Issue

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
André Kempe
Guest





PostPosted: Sat Aug 27, 2005 7:29 pm    Post subject: Template Parameter Issue Reply with quote



Hi,

I have the following problem with template-parameters under Visual
Studio 6.0.

I have to add an incrementing value to a range of elements, that means I
have an iterator it , it in ]begin;begin+size], and want to add to each
and every element of that range *it += distance(begin,it)*value.

I wrote a simple template function for that, the size is provided as a
template-parameter, since the application is somewhat short of runtime Wink.

template< unsigned size , class InputIt , class T >
void
incrementing_add( InputIt begin , T value );

I use it for arrays of varying size, and I had a bunch of problems with
that function, since it never worked the way I expected. When I finally
had a look at the assembler-code the compiler produced, I discovered
that when I use 2 for size, and in a later call 3 ( other
template-paramters remain the same ), it produces code that *always*
compares against size 2!

I use I this way, i.e.:
incrementing_add< 3 >( int* , 512 );

Is there something I am missing about explicit template-parameters? Or
have i met just another microsoft-speciality??

Greetings & TIA
André

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kaufmann@cs.uni-potsdam.d
Guest





PostPosted: Sun Aug 28, 2005 1:36 pm    Post subject: Re: Template Parameter Issue Reply with quote



Quote:
I have the following problem with template-parameters under Visual
Studio 6.0.

I have to add an incrementing value to a range of elements, that means I
have an iterator it , it in ]begin;begin+size], and want to add to each
and every element of that range *it += distance(begin,it)*value.

I wrote a simple template function for that, the size is provided as a
template-parameter, since the application is somewhat short of runtime Wink.

template< unsigned size , class InputIt , class T
void
incrementing_add( InputIt begin , T value );

I use it for arrays of varying size, and I had a bunch of problems with
that function, since it never worked the way I expected. When I finally
had a look at the assembler-code the compiler produced, I discovered
that when I use 2 for size, and in a later call 3 ( other
template-paramters remain the same ), it produces code that *always*
compares against size 2!

I use I this way, i.e.:
incrementing_add< 3 >( int* , 512 );

Is there something I am missing about explicit template-parameters? Or
have i met just another microsoft-speciality??

That is a bug in the MSVC 6.0
See: http://support.microsoft.com/kb/240871/EN-US/

To workaround this problem you can add a default-parameter to the
function that depends on the non-type parameter size.
E.g.

template <int size>
struct Workaround {};

template< unsigned size , class InputIt , class T >
void
incrementing_add( InputIt begin , T value, Workaround<size>* = 0);

That should do the trick.

Regards,
- Ben


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.