A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://timsong-cpp.github.io/cppwp/n4659/dcl.constexpr below:

[dcl.constexpr]

10 Declarations [dcl.dcl] 10.1 Specifiers [dcl.spec] 10.1.5 The constexpr specifier [dcl.constexpr]

The constexpr specifier shall be applied only to the definition of a variable or variable template or the declaration of a function or function template. A function or static data member declared with the constexpr specifier is implicitly an inline function or variable ([dcl.inline]). If any declaration of a function or function template has a constexpr specifier, then all its declarations shall contain the constexpr specifier. [Note: An explicit specialization can differ from the template declaration with respect to the constexpr specifier. end note] [Note: Function parameters cannot be declared constexpr.end note] [Example:

constexpr void square(int &x);  constexpr int bufsz = 1024;     constexpr struct pixel {          int x;
  int y;
  constexpr pixel(int);         };
constexpr pixel::pixel(int a)
  : x(a), y(x)                    { square(x); }
constexpr pixel small(2);                                       
constexpr void square(int &x) {   x *= x;
}
constexpr pixel large(4);       int next(constexpr int x) {          return x + 1;
}
extern constexpr int memsz;     

end example]

A constexpr specifier used in the declaration of a function that is not a constructor declares that function to be a constexpr function. Similarly, a constexpr specifier used in a constructor declaration declares that constructor to be a constexpr constructor.

The definition of a constexpr function shall satisfy the following requirements:

[Example:

constexpr int square(int x)
  { return x * x; }             constexpr long long_max()
  { return 2147483647; }        constexpr int abs(int x) {
  if (x < 0)
    x = -x;
  return x;                     }
constexpr int first(int n) {
  static int value = n;           return value;
}
constexpr int uninit() {
  int a;                          return a;
}
constexpr int prev(int x)
  { return --x; }               constexpr int g(int x, int n) {   int r = 1;
  while (--n > 0) r *= x;
  return r;
}

end example]

The definition of a constexpr constructor shall satisfy the following requirements:

In addition, either its function-body shall be = delete, or it shall satisfy the following requirements:

[Example:

struct Length {
  constexpr explicit Length(int i = 0) : val(i) { }
private:
  int val;
};

end example]

For a constexpr function or constexpr constructor that is neither defaulted nor a template, if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression, or, for a constructor, a constant initializer for some object ([basic.start.static]), the program is ill-formed, no diagnostic required. [Example:

constexpr int f(bool b)
  { return b ? throw 0 : 0; }           constexpr int f() { return f(true); }   
struct B {
  constexpr B(int x) : i(0) { }           int i;
};

int global;

struct D : B {
  constexpr D() : B(global) { }                                                 };

end example]

If the instantiated template specialization of a constexpr function template or member function of a class template would fail to satisfy the requirements for a constexpr function or constexpr constructor, that specialization is still a constexpr function or constexpr constructor, even though a call to such a function cannot appear in a constant expression. If no specialization of the template would satisfy the requirements for a constexpr function or constexpr constructor when considered as a non-template function or constructor, the template is ill-formed, no diagnostic required.

A call to a constexpr function produces the same result as a call to an equivalent non-constexpr function in all respects except that

The constexpr specifier has no effect on the type of a constexpr function or a constexpr constructor. [Example:

constexpr int bar(int x, int y)             { return x + y + x*y; }
int bar(int x, int y)                       { return x * 2 + 3 * y; }

end example]

A constexpr specifier used in an object declaration declares the object as const. Such an object shall have literal type and shall be initialized. In any constexpr variable declaration, the full-expression of the initialization shall be a constant expression. [Example:

struct pixel {
  int x, y;
};
constexpr pixel ur = { 1294, 1024 };    constexpr pixel origin;                 

end example]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4