failing tmpl inst of inner classes

Stefan Schwarzer sts@ica1.uni-stuttgart.de
Mon Feb 22 14:26:00 GMT 1999


// 
// I am trying to use a non-template inner class of a template class. 
// If I read the standard correctly, the code below demonstrates a bug.
//
// The code compiles without complaints and gives the expected results
// (sum of the numbers in [] brackets) on EDG based compilers (cxx and KCC).
// Since apart from line number info the preprocessor output is identical, 
// I won't include it.
// 
// (egcs-1.1.2 prerelease 1 - sparc solaris 7) (with squangling compiled in) 
// 20:03 boa_sts:~/bugs/gnu> g++ iarr.cc
// iarr.cc: In method `struct Outer<1>::Inner Outer<2>::operator []<2>(int) const':
// iarr.cc:55:   instantiated from here
// iarr.cc:28: conversion from `Outer<2>::Inner' to non-scalar type `Outer<1>::Inner' requested

template <int N>
struct Outer{
  struct Inner{
    Inner(int n): sum(n){}

    typename Outer<N-1>::Inner operator[](int n) const
    { return Outer<N-1>::Inner(sum + n); }

    int sum;
  };

  typename Outer<N-1>::Inner operator[](int n) const
  { return Outer<N-1>::Inner(n); }
};


// specializations for N==1
template<>
struct Outer<1> { 
  struct Inner {
    Inner(int n): sum(n){}

    int operator[](int n) const 
    { return sum+n; }
    
    int sum;
  };

  int operator[](int n) const
  { return n; }
};  


int main()
{
  Outer<1>  sum1;
  //std::cout << sum1[1] << "\n";
  Outer<2>  sum2;
  //std::cout << sum2[1][1] << "\n";
  return sum1[1] + sum2[1][1];
}
  














More information about the Gcc-bugs mailing list