delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/22/18:45:33

Message-ID: <335D342F.3061@mcs.nl>
Date: Tue, 22 Apr 1997 23:57:04 +0200
From: Peter Zijlstra <peter AT mcs DOT nl>
Reply-To: peter AT mcs DOT nl
Organization: Zembla
MIME-Version: 1.0
To: DJGPP mailing list <djgpp AT delorie DOT com>
Subject: HELP - weird TEMPLATE problem

Hi ye'all,

consider the following:

// template for a N-dimensional vector type
// with: D - the number of dimensions
//       T - the type of elements

template< const int D, class T>
class part_of_vector_template {

protected:
  T e[ D];

public:
  part_of_vector_template & operator *=( double arg_d) {

    for ( register int i =0; i < D; i++)
      e[ i] *= arg_d;	// **warning**

    return *this;
  };

  part_of_vector_template
  operator +( register const part_of_vector_template & arg_v) const
return t; { // **error**
                                     // nifty GNU C++ extension
----^^^^^^^^ 
    for ( register int i =0; i < D; i++)
      t.e[ i] = e[ i] + arg_v.e[ i];
  };
};


The first operator ( *=, that is) generates a warning when: 
  class part_of_vector_template<whatever, int>;

warning:
  storing double in int or something like that, writing:
    e[ i] = (int)(e[ i] * arg_d);
  solves it, but hey what's that nifty *= operator for ??

The second operator ( +, never would've guessed it right) generates an
error, 
for which I don't know the solution.

writing:

...

part_of_vector_template
operator +( register const part_of_vector_template & arg_v) const {
  part_of_vector_template t;

  ...

  return t;
};

...

would ofcourse work, but with much less efficent code. Can this named
return C++ extension only be applied on regular functions, and not on
member-functions or is it the template aspect of the whole thingy that
fuckes it up ??? 

Has anyone got some useful to say about this (most iritating) problem..

Thnx,

Peter

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019