C++程序  |  133行  |  3.25 KB

// RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify %s 

namespace Test1 {

struct B {
  virtual void f(int);
};

struct D : B {
  virtual void f(long) override; // expected-error {{'f' marked 'override' but does not override any member functions}}
  void f(int) override;
};
}

namespace Test2 {

struct A {
  virtual void f(int, char, int);
};

template<typename T>
struct B : A {
  // FIXME: Diagnose this.
  virtual void f(T) override;
};

template<typename T>
struct C : A {
  virtual void f(int) override; // expected-error {{does not override}}
};

}

namespace Test3 {

struct A {
  virtual void f(int, char, int);
};

template<typename... Args>
struct B : A { 
  virtual void f(Args...) override; // expected-error {{'f' marked 'override' but does not override any member functions}}
};

template struct B<int, char, int>;
template struct B<int>; // expected-note {{in instantiation of template class 'Test3::B<int>' requested here}}

}

namespace Test4 {
struct B {
  virtual void f() const final; // expected-note {{overridden virtual function is here}}
};

struct D : B {
  void f() const; // expected-error {{declaration of 'f' overrides a 'final' function}}
};

}

namespace PR13499 {
  struct X {
    virtual void f();
    virtual void h();
  };
  template<typename T> struct A : X {
    void f() override;
    void h() final;
  };
  template<typename T> struct B : X {
    void g() override; // expected-error {{only virtual member functions can be marked 'override'}}
    void i() final; // expected-error {{only virtual member functions can be marked 'final'}}
  };
  B<int> b; // no-note
  template<typename T> struct C : T {
    void g() override;
    void i() final;
  };
  template<typename T> struct D : X {
    virtual void g() override; // expected-error {{does not override}}
    virtual void i() final;
  };
  template<typename...T> struct E : X {
    void f(T...) override;
    void g(T...) override; // expected-error {{only virtual member functions can be marked 'override'}}
    void h(T...) final;
    void i(T...) final; // expected-error {{only virtual member functions can be marked 'final'}}
  };
  // FIXME: Diagnose these in the template definition, not in the instantiation.
  E<> e; // expected-note {{in instantiation of}}

  template<typename T> struct Y : T {
    void f() override;
    void h() final;
  };
  template<typename T> struct Z : T {
    void g() override; // expected-error {{only virtual member functions can be marked 'override'}}
    void i() final; // expected-error {{only virtual member functions can be marked 'final'}}
  };
  Y<X> y;
  Z<X> z; // expected-note {{in instantiation of}}
}

namespace MemberOfUnknownSpecialization {
  template<typename T> struct A {
    struct B {};
    struct C : B {
      void f() override;
    };
  };

  template<> struct A<int>::B {
    virtual void f();
  };
  // ok
  A<int>::C c1;

  template<> struct A<char>::B {
    void f();
  };
  // expected-error@-13 {{only virtual member functions can be marked 'override'}}
  // expected-note@+1 {{in instantiation of}}
  A<char>::C c2;

  template<> struct A<double>::B {
    virtual void f() final;
  };
  // expected-error@-20 {{declaration of 'f' overrides a 'final' function}}
  // expected-note@-3 {{here}}
  // expected-note@+1 {{in instantiation of}}
  A<double>::C c3;
}