While 3.4 made substantial improvements on handling of template parameters,
there are still some incorrect compiler errors that are triggered with a
combination of complexity in the template arguments and levels of templates. 

In the testcase below, gcc will error out with:

  t.cxx: In function ‘void foo_function(ch_t, ch_t, ch_t*)’:
  t.cxx:20: error: ‘(((int)false) + 7)’ is not a valid template argument for
type ‘int’ because it is a non-constant expression
  t.cxx:20: error: no match for ‘operator+’ in ‘A + B’

The error goes away if the function foo_function (that calls the
my_int::operator +) is un-templatized. Note that the template parameter for
foo_function is not even used anywhere in the function so it should have no
impact. Simplifying the expression for the template parameters on the return
type of my_int::operator + also makes the error go away.


#define MAX(a,b) ((a) > (b) ? (a) : (b))
                                                                               
                                                                    
template<int W, bool S>
class mc_int {
public:
  int d;
  mc_int() { d = 0; }
  mc_int(int x) { d = x; }
  mc_int(const mc_int &x) { d = x.d; }
  template<int W2, bool S2>
  mc_int<MAX(W+(S2&&!S),W2+(S&&!S2))+1, (S||S2)> operator +(const mc_int<W2,S2>
& x) const {
    return 0;
  }
};
                                                                               
                                                                    
typedef mc_int<6,false> ch_t;
                                                                               
                                                                    
template <int footemplate>
void foo_function(ch_t A, ch_t B, ch_t *C) {
    A + B;
}
                                                                               
                                                                    
int main() {
  ch_t fooA, fooB, fooC, fooD, fooE;
  fooA= (ch_t) 5;
  fooB= (ch_t) 15;
  ch_t CC;
  foo_function<0>(fooA, fooB, &CC);
}

-- 
           Summary: compilation error when template parameters have certain
                    complexity
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: critical
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: andres_takach at mentor dot com
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: gcc version 4.0.1
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23789

Reply via email to