This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/13089] New: bug using operator[] for a template class & ISO C++ standard


Hi there !

First of all, the context :
~>  uname -a
Linux machine 2.4.20-3-686 #1 Sat Jun 7 22:34:55 EST 2003 i686 GNU/Linux
~> g++ -v
Reading specs from
/logiciels/public/gcc-3.3.1/lib/gcc-lib/i686-pc-linux-gnu/3.3.1/specs
Configured with: ../gcc-3.3.1/configure --prefix=/logiciels/public/gcc-3.3.1
Thread model: posix
gcc version 3.3.1


Now, the short piece of code :
---8<-------------------- BEGINNING OF foo.cc ------------------------------
#include <iostream>

template <class T, int N> class Array
{
  private :
    T t_ [N];

  public :
    Array (const T& t = T ()) {raz (t);};
    Array (const Array<T, N>& a) {
      *this = a;
    };
    Array<T, N>& operator= (const Array<T, N>& a) {
      if (this != &a)
        for (size_t i = 0; i < N; i++)
          t_ [i] = a [i];
      return *this;
    };
    ~Array () {};
    void raz (const T& t = T ()) {
      for (size_t i = 0; i < N; i++)
        t_ [i] = t;
    }
    T& operator [] (size_t n) {
      return t_ [n];
    };
    const T& operator [] (size_t n) const {
      return t_ [n];
    };
    operator const T* () const {
      return t_;
    };
};

int main ()
{
  const Array<float, 48> tabDef;
  float valDef = tabDef[0];

  return 0;
}
---8<-------------------------- END OF foo.cc -------------------------------


Now, this is what the compiler gives :
~> g++ -c foo.cc
foo.cc: In function `int main()':
foo.cc:39: error: ISO C++ says that `const T& Array<T, N>::operator[](unsigned 
   int) const [with T = float, int N = 48]' and `operator[]' are ambiguous even 
   though the worst conversion for the former is better than the worst 
   conversion for the latter
foo.cc:39: error: ISO C++ says that `const T& Array<T, N>::operator[](unsigned 
   int) const [with T = float, int N = 48]' and `operator[]' are ambiguous even 
   though the worst conversion for the former is better than the worst 
   conversion for the latter

To me, the reason given by the compiler is rather obscure. I do not see 
any rule in the ISO C++ standard refering to this kind of ambiguity.
Anyway, the most striking thing is that if I comment the "operator const T* 
() const" operator, the compiler does not complain anymore.

If this is no bug, it really looks like one...
Thanks in advance for your help !

Olivier Despouys

-- 
           Summary: bug using operator[] for a template class & ISO C++
                    standard
           Product: gcc
           Version: 3.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: olivier dot despouys at rte-france dot com
                CC: gcc-bugs at gcc dot gnu dot org


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]