コード例 #1
0
ファイル: p7-0x.cpp プロジェクト: 4ntoine/clang
// * from an integer type or unscoped enumeration type to an integer type that
//   cannot represent all the values of the original type, except where the
//   source is a constant expression and the actual value after conversion will
//   fit into the target type and will produce the original value when converted
//   back to the original type.
void shrink_int() {
  // Not a constant expression.
  short s = 1;
  unsigned short us = 1;
  Agg<char> c1 = {s};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
  Agg<unsigned short> s1 = {s};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
  Agg<short> s2 = {us};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}

  // "that cannot represent all the values of the original type" means that the
  // validity of the program depends on the relative sizes of integral types.
  // This test compiles with -m64, so sizeof(int)<sizeof(long)==sizeof(long
  // long).
  long l1 = 1;
  Agg<int> i1 = {l1};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
  long long ll = 1;
  Agg<long> l2 = {ll};  // OK

  // Constants.
  Agg<char> c2 = {127};  // OK
  Agg<char> c3 = {300};  // expected-error {{ cannot be narrowed }} expected-note {{silence}} expected-warning {{changes value}}

  Agg<int> i2 = {0x7FFFFFFFU};  // OK
  Agg<int> i3 = {0x80000000U};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
  Agg<unsigned int> i4 = {-0x80000000L};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}

  // Bool is also an integer type, but conversions to it are a different AST
  // node.
  Agg<bool> b1 = {0};  // OK
  Agg<bool> b2 = {1};  // OK
  Agg<bool> b3 = {-1};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}

  // Conversions from pointers to booleans aren't narrowing conversions.
  Agg<bool>* ptr = &b1;
  Agg<bool> b = {ptr};  // OK

  Agg<short> ce1 = { Convert<int>(100000) }; // expected-error {{constant expression evaluates to 100000 which cannot be narrowed to type 'short'}} expected-note {{silence}} expected-warning {{changes value from 100000 to -31072}}
  Agg<char> ce2 = { ConvertVar<short>() }; // expected-error {{non-constant-expression cannot be narrowed from type 'short' to 'char'}} expected-note {{silence}}

  // Negative -> larger unsigned type.
  unsigned long long ll1 = { -1 }; // expected-error {{ -1 which cannot be narrowed}} expected-note {{silence}}
  unsigned long long ll2 = { 1 }; // OK
  unsigned long long ll3 = { s }; // expected-error {{cannot be narrowed from type 'short'}} expected-note {{silence}}
  unsigned long long ll4 = { us }; // OK
  unsigned long long ll5 = { ll }; // expected-error {{cannot be narrowed from type 'long long'}} expected-note {{silence}}
  Agg<unsigned long long> ll6 = { -1 }; // expected-error {{ -1 which cannot be narrowed}} expected-note {{silence}}
  Agg<unsigned long long> ll7 = { 18446744073709551615ULL }; // OK
  Agg<unsigned long long> ll8 = { __int128(18446744073709551615ULL) + 1 }; // expected-error {{ 18446744073709551616 which cannot be narrowed}} expected-note {{silence}} expected-warning {{changes value}}
  signed char c = 'x';
  unsigned short usc1 = { c }; // expected-error {{non-constant-expression cannot be narrowed from type 'signed char'}} expected-note {{silence}}
  unsigned short usc2 = { (signed char)'x' }; // OK
  unsigned short usc3 = { (signed char)-1 }; // expected-error {{ -1 which cannot be narrowed}} expected-note {{silence}}
}
コード例 #2
0
ファイル: p146.cpp プロジェクト: ftiasch/acm-icpc
inline LL mul_mod(LL a, LL b, LL mod) {
    return __int128(a) * b % mod;
}