Template error in gcc 3.0 (works in 2.95.2)

Scott MacDonald smacdonald@seimac.com
Tue Jun 19 05:44:00 GMT 2001


Hi,

g++ 3.0 will not compile the code at the end of this message.  I am using
g++ 3.0 on Redhat Linux 6.2.
The tarball was downloaded and built with no special flags.

The code works fine using g++ ( 2.95.2 on Redhat 6.2), aCC (3.27 on HP-UX
11.0), and
Comeau C++ (4.2.45.2 online version).

This was the results of a compile:
    > g++ gcc3units.cc
    > gcc3units.cc: In function `int main()':
    > gcc3units.cc:27: non-constant `-0' cannot be used as template argument
    > gcc3units.cc:27: no match for `const unit<1, 0>& / const unit<2, 0>&'
operator

In this example all of the template parameters are either literals or as a
direct result of subtracting
other template parameters.  None of the parameters should be non-constant.

As an interesting note if you only use one template parameter instead of two
it compiles fine.
To see the results of this compile the code with:
    g++ -DONE_ONLY gcc3units.cc

I hope this helps and I am looking forward to using the new release.

Scott MacDonald

code below
--------------------------------------------

#ifndef ONE_ONLY

template < int I1, int I2 >
class unit
{
public:
  unit() {}
  unit( const unit<I1,I2>& ) {}

  template< int Q1, int Q2 >
  unit< I1 - Q1, I2 - Q2 > operator / ( const unit< Q1, Q2 >& rhs ) const {
    return unit< I1 - Q1, I2 - Q2 >();
  }

};

int main()
{
  const unit<1,0> u1;
  const unit<2,0> u2;

  unit<-1,0> u3( u1 / u2 );
}

#else

template < int I1 >
class unit
{
public:
  unit() {}
  unit( const unit<I1>& ) {}

  template< int Q1 >
  unit< I1 - Q1 > operator / ( const unit< Q1 >& rhs ) const {
    return unit< I1 - Q1 >();
  }

};

int main()
{
  const unit<1> u1;
  const unit<2> u2;

  unit<-1> u3( u1 / u2 );
}

#endif




More information about the Gcc-bugs mailing list