Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      contract_assert statement(since C++26)

      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
       
       

      Acontract_assert statement is a contract assertion that may appear in a function or lambda body to verify an internal condition. It ensures the condition holds during execution, triggering a violation (e.g. termination) in debug builds if the condition evaluates tofalse or the evaluation exits via an exception, and can be ignored in release builds for performance.

      Contents

      [edit]Syntax

      contract_assertattr (optional)(predicate);
      attr - any number ofattributes
      predicate - boolean expression that should evaluate totrue

      [edit]Keywords

      contract_assert

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_contracts202502L(C++26)Contracts

      [edit]Example

      Thecontract_assert ensures that vector's norm is positive and eithernormal or subnormal.

      template<std::floating_point T>constexprauto normalize(std::array<T,3> vector)noexcept    pre(/* is_normalizable(vector) */)    post(/* vector: is_normalized(vector) */){auto&[x, y, z]{vector};constauto norm{std::hypot(x, y, z)}; // debug check for normalization safety    contract_assert(std::isfinite(norm)&& norm> T(0));     x/= norm, y/= norm, z/= norm; return vector;}

      [edit]Support status

      C++26 feature

       
      Paper(s)

       
      GCC
      Clang
      MSVC
      Apple Clang
      EDG eccp
      Intel C++
      Nvidia HPC C++ (ex PGI)*
      Nvidia nvcc
      Cray


      Contracts  (FTM)*P2900R14

      [edit]References

      • C++26 standard (ISO/IEC 14882:2026):
      • 8.(7+c ) Assertion statement [stmt.contract.assert]

      [edit]See also

      aborts the program if the user-specified condition is nottrue. May be disabled for release builds.
      (function macro)[edit]
      Contract assertions(C++26) specifies properties that must hold at certain points during execution[edit]
      static_assert declaration(C++11) performs compile-time assertion checking[edit]
      function contract specifiers(C++26) specifies preconditions (pre) and postconditions (post)[edit]
      [[assume(expression)]]
      (C++23)
      specifies that theexpression will always evaluate totrue at a given point
      (attribute specifier)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/contract_assert&oldid=182023"

      [8]ページ先頭

      ©2009-2025 Movatter.jp