summaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/warn/Warray-bounds-4.C
blob: 319038a73bf8ec06c3f86841d8fea70e5fa309c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// { dg-do compile }
// { dg-options "-O2 -Warray-bounds" }

class String
{
public:
  virtual unsigned long length() const = 0;
  virtual char get(unsigned long index) const = 0;
  virtual void set(unsigned long index, char value) = 0;
  virtual char& operator[] (unsigned long value) = 0;
  virtual ~String() {};
};

template<unsigned long size> class FixedString : public String
{
private:
  char contents[size];

public:
  virtual unsigned long length() const { return size; }
  virtual char get(unsigned long index) const { return contents[index]; }
  virtual void set(unsigned long index, char value) { contents[index] = value; }
  virtual char& operator[] (unsigned long index) { return contents[index]; }

  FixedString() { contents[0] = '\0'; } // { dg-warning "above array bounds" }
};

void print_length (const String& string);

int main()
{
  const FixedString<0> empty;

  print_length(empty);

  return 0;
}