Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      static_assert declaration(since C++11)

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      Storage duration specifiers
      Initialization
      Expressions
      Alternative representations
      Literals
      Boolean -Integer -Floating-point
      Character -String -nullptr(C++11)
      User-defined(C++11)
      Utilities
      Attributes(C++11)
      Types
      typedef declaration
      Type alias declaration(C++11)
      Casts
      Memory allocation
      Classes
      Class-specific function properties
      Special member functions
      Templates
      Miscellaneous
       
      Declarations
       

      Performs compile-time assertion checking.

      Contents

      [edit]Syntax

      static_assert(bool-constexpr,unevaluated-string) (1)
      static_assert(bool-constexpr) (2)(since C++17)
      static_assert(bool-constexpr,constant-expression) (3)(since C++26)

      Declares a static assertion. If the assertion fails, the program is ill-formed, and a diagnostic error message may be generated.

      1) A static assertion with fixed error message.
      2) A static assertion without error message.
      3) A static assertion with user-generated error message.
      This syntax can only be matched if syntax(1) does not match.

      [edit]Explanation

      bool-constexpr -

      acontextually converted constant expression of typebool. Built-in conversions are not allowed, except for non-narrowingintegral conversions tobool.

      (until C++23)

      an expressioncontextually converted tobool where the conversion is aconstant expression

      (since C++23)
      unevaluated-string - anunevaluated string literal that will appear as the error message
      constant-expression - aconstant expressionmsg satisfying all following conditions:
      • msg.size() is implicitly convertible tostd::size_t.
      • msg.data() is implicitly convertible toconstchar*.

      Astatic_assert declaration may appear at namespace and blockscope (as ablock declaration) and inside a class body (as amember declaration).

      Ifbool-constexpr is well-formed and evaluates totrue, or is evaluated in the context of a template definition and the template is uninstantiated, this declaration has no effect. Otherwise a compile-time error is issued, and the user-provided message, if any, is included in the diagnostic message.

      The text of the user-provided message is determined as follows:

      • If the message matches the syntactic requirements ofunevaluated-string, the text of the message is the text of theunevaluated-string.
      • Otherwise, given the following values:
      The text of the message is formed by the sequence oflencode units, starting atptr, of theordinary literal encoding. For each integeri in[0len),ptr[i] must be anintegral constant expression.
      (since C++26)

      [edit]Notes

      The standard does not require a compiler to print the verbatim text oferror message, though compilers generally do so as much as possible.

      Since the error message has to be a string literal, it cannot contain dynamic information or even aconstant expression that is not a string literal itself. In particular, it cannot contain thename of thetemplate type argument.

      (until C++26)
      Feature-test macroValueStdFeature
      __cpp_static_assert200410L(C++11)static_assert (syntax(1))
      201411L(C++17)Single-argumentstatic_assert (syntax(2))
      202306L(C++26)User-generated error messages (syntax(3))

      [edit]Keywords

      static_assert

      [edit]Example

      Run this code
      #include <format>#include <type_traits> static_assert(03301==1729);// since C++17 the message string is optional template<class T>void swap(T& a, T& b)noexcept{    static_assert(std::is_copy_constructible_v<T>,"Swap requires copying");    static_assert(std::is_nothrow_copy_constructible_v<T>&&std::is_nothrow_copy_assignable_v<T>,"Swap requires nothrow copy/assign");auto c= b;    b= a;    a= c;} template<class T>struct data_structure{    static_assert(std::is_default_constructible_v<T>,"Data structure requires default-constructible elements");}; template<class>constexprbool dependent_false=false;// workaround before CWG2518/P2593R1 template<class T>struct bad_type{    static_assert(dependent_false<T>,"error on instantiation, workaround");    static_assert(false,"error on instantiation");// OK because of CWG2518/P2593R1}; struct no_copy{    no_copy(const no_copy&)= delete;    no_copy()=default;}; struct no_default{    no_default()= delete;}; #if __cpp_static_assert >= 202306L// Not real C++ yet (std::format should be constexpr to work):static_assert(sizeof(int)==4,std::format("Expected 4, got {}", sizeof(int)));#endif int main(){int a, b;    swap(a, b);     no_copy nc_a, nc_b;    swap(nc_a, nc_b);// 1 [[maybe_unused]] data_structure<int> ds_ok;[[maybe_unused]] data_structure<no_default> ds_error;// 2}

      Possible output:

      1: error: static assertion failed: Swap requires copying2: error: static assertion failed: Data structure requires default-constructible elements3: error: static assertion failed: Expected 4, got 2

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 2039C++11only the expression before conversion is required to be constantthe conversion must also be
      valid in a constant expression
      CWG 2518
      (P2593R1)
      C++11uninstantiatedstatic_assert(false,""); was ill-formedmade well-formed

      [edit]References

      • C++23 standard (ISO/IEC 14882:2024):
      • 9.1 Preamble [dcl.pre] (p: 10)
      • C++20 standard (ISO/IEC 14882:2020):
      • 9.1 Preamble [dcl.pre] (p: 6)
      • C++17 standard (ISO/IEC 14882:2017):
      • 10 Declarations [dcl.dcl] (p: 6)
      • C++14 standard (ISO/IEC 14882:2014):
      • 7 Declarations [dcl.dcl] (p: 4)
      • C++11 standard (ISO/IEC 14882:2011):
      • 7 Declarations [dcl.dcl] (p: 4)

      [edit]See also

      shows the given error message and renders the program ill-formed
      (preprocessing directive)[edit]
      aborts the program if the user-specified condition is nottrue. May be disabled for release builds.
      (function macro)[edit]
      contract_assert statement(C++26) verifies an internal condition during execution[edit]
      (C++11)
      conditionallyremoves a function overload or template specialization from overload resolution
      (class template)[edit]
      Type traits(C++11) define compile-time template-based interfaces to query the properties of types
      C documentation forStatic assertion
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/static_assert&oldid=181937"

      [8]ページ先頭

      ©2009-2025 Movatter.jp