Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -verify -std=c++11 %s
      2 
      3 namespace RedeclAliasTypedef {
      4   template<typename U> using T = int;
      5   template<typename U> using T = int;
      6   template<typename U> using T = T<U>;
      7 }
      8 
      9 namespace IllegalTypeIds {
     10   template<typename U> using A = void(int n = 0); // expected-error {{default arguments can only be specified for parameters in a function declaration}}
     11   template<typename U> using B = inline void(int n); // expected-error {{type name does not allow function specifier}}
     12   template<typename U> using C = virtual void(int n); // expected-error {{type name does not allow function specifier}}
     13   template<typename U> using D = explicit void(int n); // expected-error {{type name does not allow function specifier}}
     14   template<typename U> using E = void(int n) throw(); // expected-error {{exception specifications are not allowed in type aliases}}
     15   template<typename U> using F = void(*)(int n) &&; // expected-error {{pointer to function type cannot have '&&' qualifier}}
     16   template<typename U> using G = __thread void(int n); // expected-error {{type name does not allow storage class to be specified}}
     17   template<typename U> using H = constexpr int; // expected-error {{type name does not allow constexpr specifier}}
     18 
     19   template<typename U> using Y = void(int n); // ok
     20   template<typename U> using Z = void(int n) &&; // ok
     21 }
     22 
     23 namespace IllegalSyntax {
     24   template<typename Z> using ::T = void(int n); // expected-error {{name defined in alias declaration must be an identifier}}
     25   template<typename Z> using operator int = void(int n); // expected-error {{name defined in alias declaration must be an identifier}}
     26   template<typename Z> using typename U = void; // expected-error {{name defined in alias declaration must be an identifier}}
     27   template<typename Z> using typename ::V = void(int n); // expected-error {{name defined in alias declaration must be an identifier}}
     28   template<typename Z> using typename ::operator bool = void(int n); // expected-error {{name defined in alias declaration must be an identifier}}
     29 }
     30 
     31 namespace VariableLengthArrays {
     32   template<typename Z> using T = int[42]; // ok
     33 
     34   int n = 32;
     35   template<typename Z> using T = int[n]; // expected-error {{variable length array declaration not allowed at file scope}}
     36 
     37   const int m = 42;
     38   template<typename Z> using U = int[m]; // expected-note {{previous definition}}
     39   template<typename Z> using U = int[42]; // ok
     40   template<typename Z> using U = int; // expected-error {{type alias template redefinition with different types ('int' vs 'int [42]')}}
     41 }
     42 
     43 namespace RedeclFunc {
     44   int f(int, char**);
     45   template<typename Z> using T = int;
     46   T<char> f(int, char **); // ok
     47 }
     48 
     49 namespace LookupFilter {
     50   namespace N { template<typename U> using S = int; }
     51   using namespace N;
     52   template<typename U> using S = S<U>*; // ok
     53 }
     54 
     55 namespace InFunctions {
     56   template<typename...T> struct S0 {
     57     template<typename Z> using U = T*; // expected-error {{declaration type contains unexpanded parameter pack 'T'}}
     58     U<char> u;
     59   };
     60 
     61   template<typename Z> using T1 = int;
     62   template<typename Z> using T2 = int[-1]; // expected-error {{array size is negative}}
     63   template<typename...T> struct S3 { // expected-note {{template parameter is declared here}}
     64     template<typename Z> using T = int; // expected-error {{declaration of 'T' shadows template parameter}}
     65   };
     66   template<typename Z> using Z = Z;
     67 }
     68 
     69 namespace ClassNameRedecl {
     70   class C0 {
     71     // FIXME: this diagnostic is pretty poor
     72     template<typename U> using C0 = int; // expected-error {{name defined in alias declaration must be an identifier}}
     73   };
     74   class C1 {
     75     // FIXME: this diagnostic is pretty poor
     76     template<typename U> using C1 = C1; // expected-error {{name defined in alias declaration must be an identifier}}
     77   };
     78   class C2 {
     79     template<typename U> using C0 = C1; // ok
     80   };
     81   template<typename...T> class C3 {
     82     template<typename U> using f = T; // expected-error {{declaration type contains unexpanded parameter pack 'T'}}
     83   };
     84   template<typename T> class C4 { // expected-note {{template parameter is declared here}}
     85     template<typename U> using T = int; // expected-error {{declaration of 'T' shadows template parameter}}
     86   };
     87   class C5 {
     88     class c; // expected-note {{previous definition}}
     89     template<typename U> using c = int; // expected-error {{redefinition of 'c' as different kind of symbol}}
     90     class d; // expected-note {{previous definition}}
     91     template<typename U> using d = d; // expected-error {{redefinition of 'd' as different kind of symbol}}
     92   };
     93   class C6 {
     94     class c { template<typename U> using C6 = int; }; // ok
     95   };
     96 }
     97 
     98 class CtorDtorName {
     99   template<typename T> using X = CtorDtorName;
    100   X<int>(); // expected-error {{expected member name}}
    101   ~X<int>(); // expected-error {{destructor cannot be declared using a type alias}}
    102 };
    103 
    104 namespace TagName {
    105   template<typename Z> using S = struct { int n; }; // expected-error {{can not be defined}}
    106   template<typename Z> using T = class { int n; }; // expected-error {{can not be defined}}
    107   template<typename Z> using U = enum { a, b, c }; // expected-error {{can not be defined}}
    108   template<typename Z> using V = struct V { int n; }; // expected-error {{'TagName::V' can not be defined in a type alias template}}
    109 }
    110 
    111 namespace StdExample {
    112   template<typename T, typename U> struct pair;
    113 
    114   template<typename T> using handler_t = void (*)(T);
    115   extern handler_t<int> ignore;
    116   extern void (*ignore)(int);
    117   // FIXME: we recover as if cell is an undeclared variable. the diagnostics are terrible!
    118   template<typename T> using cell = pair<T*, cell<T>*>; // expected-error {{use of undeclared identifier 'cell'}} \
    119                                                            expected-error {{'T' does not refer to a value}} \
    120                                                            expected-note {{declared here}} \
    121                                                            expected-error {{expected ';' after alias declaration}}
    122 }
    123 
    124 namespace Access {
    125   class C0 {
    126     template<typename Z> using U = int; // expected-note {{declared private here}}
    127   };
    128   C0::U<int> v; // expected-error {{'U' is a private member}}
    129   class C1 {
    130   public:
    131     template<typename Z> using U = int;
    132   };
    133   C1::U<int> w; // ok
    134 }
    135 
    136 namespace VoidArg {
    137   template<typename Z> using V = void;
    138   V<int> f(int); // ok
    139   V<char> g(V<double>); // expected-error {{empty parameter list defined with a type alias of 'void' not allowed}}
    140 }
    141 
    142 namespace Curried {
    143   template<typename T, typename U> struct S;
    144   template<typename T> template<typename U> using SS = S<T, U>; // expected-error {{extraneous template parameter list in alias template declaration}}
    145 }
    146 
    147 // PR12647
    148 namespace SFINAE {
    149   template<bool> struct enable_if; // expected-note 2{{here}}
    150   template<> struct enable_if<true> { using type = void; };
    151 
    152   template<typename T> struct is_enum { static constexpr bool value = __is_enum(T); };
    153 
    154   template<typename T> using EnableIf = typename enable_if<T::value>::type; // expected-error {{undefined template}}
    155   template<typename T> using DisableIf = typename enable_if<!T::value>::type; // expected-error {{undefined template}}
    156 
    157   template<typename T> EnableIf<is_enum<T>> f();
    158   template<typename T> DisableIf<is_enum<T>> f();
    159 
    160   enum E { e };
    161 
    162   int main() {
    163     f<int>();
    164     f<E>();
    165   }
    166 
    167   template<typename T, typename U = EnableIf<is_enum<T>>> struct fail1 {}; // expected-note {{here}}
    168   template<typename T> struct fail2 : DisableIf<is_enum<T>> {}; // expected-note {{here}}
    169 
    170   fail1<int> f1; // expected-note {{here}}
    171   fail2<E> f2; // expected-note {{here}}
    172 }
    173