1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++11 -verify %s -fms-extensions -triple x86_64-apple-darwin9.0.0 2*f4a2713aSLionel Sambuc 3*f4a2713aSLionel Sambuc using size_t = decltype(sizeof(int)); 4*f4a2713aSLionel Sambuc enum class LitKind { 5*f4a2713aSLionel Sambuc Char, WideChar, Char16, Char32, 6*f4a2713aSLionel Sambuc CharStr, WideStr, Char16Str, Char32Str, 7*f4a2713aSLionel Sambuc Integer, Floating, Raw, Template 8*f4a2713aSLionel Sambuc }; 9*f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(char p) { return LitKind::Char; } 10*f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(wchar_t p) { return LitKind::WideChar; } 11*f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(char16_t p) { return LitKind::Char16; } 12*f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(char32_t p) { return LitKind::Char32; } 13*f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(const char *p, size_t n) { return LitKind::CharStr; } 14*f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(const wchar_t *p, size_t n) { return LitKind::WideStr; } 15*f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(const char16_t *p, size_t n) { return LitKind::Char16Str; } 16*f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(const char32_t *p, size_t n) { return LitKind::Char32Str; } 17*f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(unsigned long long n) { return LitKind::Integer; } 18*f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind(long double n) { return LitKind::Floating; } 19*f4a2713aSLionel Sambuc constexpr LitKind operator"" _kind2(const char *p) { return LitKind::Raw; } 20*f4a2713aSLionel Sambuc template<char ...Cs> constexpr LitKind operator"" _kind3() { return LitKind::Template; } 21*f4a2713aSLionel Sambuc 22*f4a2713aSLionel Sambuc static_assert('x'_kind == LitKind::Char, ""); 23*f4a2713aSLionel Sambuc static_assert(L'x'_kind == LitKind::WideChar, ""); 24*f4a2713aSLionel Sambuc static_assert(u'x'_kind == LitKind::Char16, ""); 25*f4a2713aSLionel Sambuc static_assert(U'x'_kind == LitKind::Char32, ""); 26*f4a2713aSLionel Sambuc static_assert("foo"_kind == LitKind::CharStr, ""); 27*f4a2713aSLionel Sambuc static_assert(u8"foo"_kind == LitKind::CharStr, ""); 28*f4a2713aSLionel Sambuc static_assert(L"foo"_kind == LitKind::WideStr, ""); 29*f4a2713aSLionel Sambuc static_assert(u"foo"_kind == LitKind::Char16Str, ""); 30*f4a2713aSLionel Sambuc static_assert(U"foo"_kind == LitKind::Char32Str, ""); 31*f4a2713aSLionel Sambuc static_assert(194_kind == LitKind::Integer, ""); 32*f4a2713aSLionel Sambuc static_assert(0377_kind == LitKind::Integer, ""); 33*f4a2713aSLionel Sambuc static_assert(0x5ffc_kind == LitKind::Integer, ""); 34*f4a2713aSLionel Sambuc static_assert(.5954_kind == LitKind::Floating, ""); 35*f4a2713aSLionel Sambuc static_assert(1._kind == LitKind::Floating, ""); 36*f4a2713aSLionel Sambuc static_assert(1.e-2_kind == LitKind::Floating, ""); 37*f4a2713aSLionel Sambuc static_assert(4e6_kind == LitKind::Floating, ""); 38*f4a2713aSLionel Sambuc static_assert(4e6_kind2 == LitKind::Raw, ""); 39*f4a2713aSLionel Sambuc static_assert(4e6_kind3 == LitKind::Template, ""); 40*f4a2713aSLionel Sambuc 41*f4a2713aSLionel Sambuc constexpr const char *fractional_digits_impl(const char *p) { 42*f4a2713aSLionel Sambuc return *p == '.' ? p + 1 : *p ? fractional_digits_impl(p + 1) : 0; 43*f4a2713aSLionel Sambuc } 44*f4a2713aSLionel Sambuc constexpr const char *operator"" _fractional_digits(const char *p) { 45*f4a2713aSLionel Sambuc return fractional_digits_impl(p) ?: p; 46*f4a2713aSLionel Sambuc } 47*f4a2713aSLionel Sambuc constexpr bool streq(const char *p, const char *q) { 48*f4a2713aSLionel Sambuc return *p == *q && (!*p || streq(p+1, q+1)); 49*f4a2713aSLionel Sambuc } 50*f4a2713aSLionel Sambuc 51*f4a2713aSLionel Sambuc static_assert(streq(143.97_fractional_digits, "97"), ""); 52*f4a2713aSLionel Sambuc static_assert(streq(0x786_fractional_digits, "0x786"), ""); 53*f4a2713aSLionel Sambuc static_assert(streq(.4_fractional_digits, "4"), ""); 54*f4a2713aSLionel Sambuc static_assert(streq(4._fractional_digits, ""), ""); 55*f4a2713aSLionel Sambuc static_assert(streq(1e+97_fractional_digits, "1e+97"), ""); 56*f4a2713aSLionel Sambuc static_assert(streq(0377_fractional_digits, "0377"), ""); 57*f4a2713aSLionel Sambuc static_assert(streq(0377.5_fractional_digits, "5"), ""); 58*f4a2713aSLionel Sambuc 59*f4a2713aSLionel Sambuc int operator"" _ambiguous(char); // expected-note {{candidate}} 60*f4a2713aSLionel Sambuc namespace N { 61*f4a2713aSLionel Sambuc void *operator"" _ambiguous(char); // expected-note {{candidate}} 62*f4a2713aSLionel Sambuc } 63*f4a2713aSLionel Sambuc using namespace N; 64*f4a2713aSLionel Sambuc int k = 'x'_ambiguous; // expected-error {{ambiguous}} 65*f4a2713aSLionel Sambuc 66*f4a2713aSLionel Sambuc int operator"" _deleted(unsigned long long) = delete; // expected-note {{here}} 67*f4a2713aSLionel Sambuc int m = 42_deleted; // expected-error {{attempt to use a deleted}} 68*f4a2713aSLionel Sambuc 69*f4a2713aSLionel Sambuc namespace Using { 70*f4a2713aSLionel Sambuc namespace M { 71*f4a2713aSLionel Sambuc int operator"" _using(char); 72*f4a2713aSLionel Sambuc } 73*f4a2713aSLionel Sambuc int k1 = 'x'_using; // expected-error {{no matching literal operator for call to 'operator "" _using'}} 74*f4a2713aSLionel Sambuc 75*f4a2713aSLionel Sambuc using M::operator "" _using; 76*f4a2713aSLionel Sambuc int k2 = 'x'_using; 77*f4a2713aSLionel Sambuc } 78*f4a2713aSLionel Sambuc 79*f4a2713aSLionel Sambuc namespace AmbiguousRawTemplate { 80*f4a2713aSLionel Sambuc int operator"" _ambig1(const char *); // expected-note {{candidate}} 81*f4a2713aSLionel Sambuc template<char...> int operator"" _ambig1(); // expected-note {{candidate}} 82*f4a2713aSLionel Sambuc 83*f4a2713aSLionel Sambuc int k1 = 123_ambig1; // expected-error {{call to 'operator "" _ambig1' is ambiguous}} 84*f4a2713aSLionel Sambuc 85*f4a2713aSLionel Sambuc namespace Inner { 86*f4a2713aSLionel Sambuc template<char...> int operator"" _ambig2(); // expected-note 3{{candidate}} 87*f4a2713aSLionel Sambuc } 88*f4a2713aSLionel Sambuc int operator"" _ambig2(const char *); // expected-note 3{{candidate}} 89*f4a2713aSLionel Sambuc using Inner::operator"" _ambig2; 90*f4a2713aSLionel Sambuc 91*f4a2713aSLionel Sambuc int k2 = 123_ambig2; // expected-error {{call to 'operator "" _ambig2' is ambiguous}} 92*f4a2713aSLionel Sambuc 93*f4a2713aSLionel Sambuc namespace N { 94*f4a2713aSLionel Sambuc using Inner::operator"" _ambig2; 95*f4a2713aSLionel Sambuc 96*f4a2713aSLionel Sambuc int k3 = 123_ambig2; // ok 97*f4a2713aSLionel Sambuc 98*f4a2713aSLionel Sambuc using AmbiguousRawTemplate::operator"" _ambig2; 99*f4a2713aSLionel Sambuc 100*f4a2713aSLionel Sambuc int k4 = 123_ambig2; // expected-error {{ambiguous}} 101*f4a2713aSLionel Sambuc 102*f4a2713aSLionel Sambuc namespace M { 103*f4a2713aSLionel Sambuc 104*f4a2713aSLionel Sambuc template<char...> int operator"" _ambig2(); 105*f4a2713aSLionel Sambuc 106*f4a2713aSLionel Sambuc int k5 = 123_ambig2; // ok 107*f4a2713aSLionel Sambuc } 108*f4a2713aSLionel Sambuc 109*f4a2713aSLionel Sambuc int operator"" _ambig2(unsigned long long); 110*f4a2713aSLionel Sambuc 111*f4a2713aSLionel Sambuc int k6 = 123_ambig2; // ok 112*f4a2713aSLionel Sambuc int k7 = 123._ambig2; // expected-error {{ambiguous}} 113*f4a2713aSLionel Sambuc } 114*f4a2713aSLionel Sambuc } 115*f4a2713aSLionel Sambuc 116*f4a2713aSLionel Sambuc constexpr unsigned mash(unsigned a) { 117*f4a2713aSLionel Sambuc return 0x93ae27b5 * ((a >> 13) | a << 19); 118*f4a2713aSLionel Sambuc } 119*f4a2713aSLionel Sambuc template<typename=void> constexpr unsigned hash(unsigned a) { return a; } 120*f4a2713aSLionel Sambuc template<char C, char...Cs> constexpr unsigned hash(unsigned a) { 121*f4a2713aSLionel Sambuc return hash<Cs...>(mash(a ^ mash(C))); 122*f4a2713aSLionel Sambuc } 123*f4a2713aSLionel Sambuc template<typename T, T v> struct constant { constexpr static T value = v; }; 124*f4a2713aSLionel Sambuc template<char...Cs> constexpr unsigned operator"" _hash() { 125*f4a2713aSLionel Sambuc return constant<unsigned, hash<Cs...>(0)>::value; 126*f4a2713aSLionel Sambuc } 127*f4a2713aSLionel Sambuc static_assert(0x1234_hash == 0x103eff5e, ""); 128*f4a2713aSLionel Sambuc static_assert(hash<'0', 'x', '1', '2', '3', '4'>(0) == 0x103eff5e, ""); 129*f4a2713aSLionel Sambuc 130*f4a2713aSLionel Sambuc // Functions and literal suffixes go in separate namespaces. 131*f4a2713aSLionel Sambuc namespace Namespace { 132*f4a2713aSLionel Sambuc template<char...> int operator"" _x(); 133*f4a2713aSLionel Sambuc int k = _x(); // expected-error {{undeclared identifier '_x'}} 134*f4a2713aSLionel Sambuc 135*f4a2713aSLionel Sambuc int _y(unsigned long long); 136*f4a2713aSLionel Sambuc int k2 = 123_y; // expected-error {{no matching literal operator for call to 'operator "" _y'}} 137*f4a2713aSLionel Sambuc } 138*f4a2713aSLionel Sambuc 139*f4a2713aSLionel Sambuc namespace PR14950 { 140*f4a2713aSLionel Sambuc template<...> // expected-error {{expected template parameter}} 141*f4a2713aSLionel Sambuc int operator"" _b(); // expected-error {{no function template matches function template specialization}} 142*f4a2713aSLionel Sambuc int main() { return 0_b; } // expected-error {{no matching literal operator for call to 'operator "" _b'}} 143*f4a2713aSLionel Sambuc } 144