Namespaces
Variants

Boolean literals

From cppreference.net
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

目录

语法

true (1)
false (2)

说明

布尔字面量是关键字 true false 。它们是 纯右值 ,其类型为 bool

注释

请参阅 整数转换 了解从 bool 到其他类型的隐式转换,以及 布尔转换 了解从其他类型到 bool 的隐式转换。

关键词

false , true

示例

#include <iostream>
int main()
{
    std::cout << std::boolalpha
              << true << '\n'
              << false << '\n'
              << std::noboolalpha
              << true << '\n'
              << false << '\n';
}

输出:

true
false
1
0

参考文献

  • C++23 标准 (ISO/IEC 14882:2024):
  • 5.13.6 布尔字面量 [lex.bool]
  • C++20 标准 (ISO/IEC 14882:2020):
  • 5.13.6 布尔字面量 [lex.bool]
  • C++17 标准 (ISO/IEC 14882:2017):
  • 5.13.6 布尔字面量 [lex.bool]
  • C++14 标准 (ISO/IEC 14882:2014):
  • 2.13.6 布尔字面量 [lex.bool]
  • C++11 标准 (ISO/IEC 14882:2011):
  • 2.13.6 布尔字面量 [lex.bool]
  • C++98 标准 (ISO/IEC 14882:1998):
  • 2.13.5 布尔字面量 [lex.bool]

参阅

C 文档 关于 预定义布尔常量