Increment/decrement operators

来自cppreference.com

 
 
C + +语言
大会的主题
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
流量控制
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
条件执行语句
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
迭代语句
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
跳转语句
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
功能
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
函数声明
lambda函数的声明
函数模板
的历史。内嵌说明
异常规范 (过时了)
noexcept说明 (C++11)
例外
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
命名空间
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
类型
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
规范
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
CV符
存储时间说明符
constexpr说明 (C++11)
汽车符 (C++11)
alignas说明 (C++11)
初始化
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
布尔文字
nullptr (C++11)
用户定义的 (C++11)
表达式
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
另一种表示形式
实用工具
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
类型
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
声明类型别名 (C++11)
属性 (C++11)
施放
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
隐式转换
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-风格和功能转换
内存分配
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
类特定的功能特性
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
虚函数
覆盖说明 (C++11)
最后说明 (C++11)
明确的 (C++11)
静态的
特殊的成员函数
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
模板
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
类模板
函数模板
模板特化
参数包 (C++11)
杂项
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
内联汇编
 
递增/递减运算符递增或递减的对象的值.
Original:
Increment/decrement operators increments or decrements the value of the object.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Operator name Syntax Over​load​able Prototype examples (for class T)
Inside class definition Outside class definition
pre-increment ++a Yes T& T::operator++(); T& operator++(T& a);
pre-decrement --a Yes T& T::operator--(); T& operator--(T& a);
post-increment a++ Yes T T::operator++(int); T operator++(T& a, int);
post-decrement a-- Yes T T::operator--(int); T operator--(T& a, int);
注意
Original:
Notes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • 前缀形式的内置在运营商的返回引用和后缀形式返回值,和典型的用户定义的重载跟随模式,使用户定义的运算可以用在相同的方式内置插件。然而,在一个用户定义的操作符重载,任何类型都可以被用作返回类型(包括void).
    Original:
    Prefix forms of the built-in operators return references and postfix forms return values, and typical user-defined overloads follow the pattern so that the user-defined operators can be used in the same manner as the built-ins. However, in a user-defined operator overload, any type can be used as return type (including void).
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • int参数是一个虚拟参数,用来区分前和后的版本的运营商。当用户定义的后缀运算符被调用时,在该参数传递的值始终为零,虽然它可能被改变了调用函数调用表示法的操作员使用,例如a.operator++(2).
    Original:
    The int parameter is a dummy parameter used to differentiate between pre- and post- in versions of the operators. When the user-defined postfix operator is called, the value passed in that parameter is always zero, although it may be changed by calling the operator using function call notation, e.g. a.operator++(2).
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

目录

[编辑] 解释

“前增加”和“预减”运营商增加或减小该值的对象,并返回一个引用到的结果.....
Original:
pre-increment and pre-decrement operators increments or decrements the value of the object and returns a reference to the result.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
“后增加”和“减”创建一个副本的对象,递增或递减的对象的值,并返回从之前的增量或减量的副本.
Original:
post-increment and post-decrement creates a copy of the object, increments or decrements the value of the object and returns the copy from before the increment or decrement.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 内置的前缀运算符

对于每一个可以用volatile限定A以外bool,并为每个可以用volatile限定P可以用CV合格的对象类型的指针,下面的函数签名参与重载解析:算术类型
Original:
For every optionally volatile-qualified arithmetic type A other than bool, and for every optionally volatile-qualified pointer P to optionally cv-qualified object type, the following function signatures participate in overload resolution:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
A& operator++(A&)
bool& operator++(bool&)
(过时了)
P& operator++(P&)
A& operator--(A&)
P& operator--(P&)
一个内置的前缀递增或递减运算的操作数必须是一个可修改的左值(非const引用)的非布尔值的算术类型或对象类型的指针完成。这些操作数,表达++x是完全等同于x+=1--x,这是完全等同的表达x-=1,其结果是更新操作,返回的左值,所有的算术转换规则和指针运算规则定义算术运算符申请.
Original:
The operand of a built-in prefix increment or decrement operator must be a modifiable lvalue (non-const reference) of non-boolean arithmetic type or pointer to complete object type. For these operands, the expression ++x is exactly equivalent to x+=1, and the expression --x is exactly equivalent to x-=1, that is, the result is the updated operand, returned as lvalue, and all arithmetic conversion rules and pointer arithmetic rules defined for 算术运算符 apply.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
如果操作数的前递增运算符类型bool,它被设置为true(过时了).
Original:
If the operand of the preincrement operator is of type bool, it is set to true (过时了).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 内置的后缀运算符

对于每一个可以用volatile限定A以外bool,并为每个可以用volatile限定P可以用CV合格的对象类型的指针,下面的函数签名参与重载解析:算术类型
Original:
For every optionally volatile-qualified arithmetic type A other than bool, and for every optionally volatile-qualified pointer P to optionally cv-qualified object type, the following function signatures participate in overload resolution:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
A operator++(A&, int)
bool operator++(bool&, int)
(过时了)
P operator++(P&, int)
A operator--(A&, int)
P operator--(P&, int)
一个内置的后缀递增或递减运算的操作数必须是一个可修改的左值(非const引用)的非布尔值的算术类型或对象类型的指针完成。其结果是一个prvalue,这是复制的原始值的操作数。作为一个副作用,该运营商修改其参数值arg如果通过评估arg += 1arg -= 1,分别为递增和递减。所有的算术转换规则和指针运算规则定义算术运算符申请.
Original:
The operand of a built-in postfix increment or decrement operator must be a modifiable lvalue (non-const reference) of non-boolean arithmetic type or pointer to complete object type. The result is a prvalue, which is a copy the original value of the operand. As a side-effect, this operator modifies the value of its argument arg as if by evaluating arg += 1 or arg -= 1, for increment and decrement respectively. All arithmetic conversion rules and pointer arithmetic rules defined for 算术运算符 apply.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
如果类型bool的后递增操作符的操作数,它被设置为true(过时了).
Original:
If the operand of the postincrement operator is of type bool, it is set to true (过时了).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 为例

#include <iostream>
int main()
{
    int n = 1;
    int n2 = ++n;
    int n3 = ++ ++n;
    int n4 = n++;
//    int n5 = n++ ++; // compile error
//    int n5 = n + ++n; // undefined behavior
    std::cout << "n = " << n << '\n'
              << "n2 = " << n2 << '\n'
              << "n3 = " << n3 << '\n'
              << "n4 = " << n4 << '\n';
}

Output:

n = 5
n2 = 2
n3 = 4
n4 = 4

[编辑] 注释

由于副作用参与,内置的增量和减量运算,必须谨慎使用,以避免不确定的行为因违反排序规则.
Original:
Because of the side-effects involved, built-in increment and decrement operators must be used with care to avoid undefined behavior due to violations of 排序规则.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
在操作过程中,因为临时对象的拷贝构造“前增加”或“预减”运营商在不使用的上下文中返回的值通常更有效.
Original:
Because a temporary copy of the object is constructed during the operation, pre-increment or pre-decrement operators are usually more efficient in contexts where the returned value is not used.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 标准库

递增和递减运算符重载,许多标准库类型。特别是,每一个Iterator重载运算符+ +和每BidirectionalIterator重载运算符 - ,即使这些运营商没有特定的迭代器操作.
Original:
Increment and decrement operators are overloaded for many standard library types. In particular, every Iterator overloads operator++ and every BidirectionalIterator overloads operator--, even if those operators are no-ops for the particular iterator.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
算术类型的重载
Original:
overloads for arithmetic types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
递增或递减一个原子值
Original:
increments or decrements the atomic value by one
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数of std::atomic [edit]
递增或递减滴答计数
Original:
increments or decrements the tick count
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数of std::chrono::duration
迭代器类型的重载
Original:
overloads for iterator types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
进步的迭代器
Original:
advances the iterator
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数of std::raw_storage_iterator
进步的迭代器
Original:
advances the iterator
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数of std::reverse_iterator
减少了迭代器
Original:
decrements the iterator
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数of std::reverse_iterator
no-op
(公共成员函数of std::back_insert_iterator
no-op
(公共成员函数of std::front_insert_iterator
no-op
(公共成员函数of std::insert_iterator
进步的迭代器
Original:
advances the iterator
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数of std::move_iterator
减少了迭代器
Original:
decrements the iterator
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数of std::move_iterator
推进istream_iterator
Original:
advances the istream_iterator
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数of std::istream_iterator
no-op
(公共成员函数of std::ostream_iterator
推进istreambuf_iterator
Original:
advances the istreambuf_iterator
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数of std::istreambuf_iterator
no-op
(公共成员函数of std::ostreambuf_iterator
推进regex_iterator
Original:
advances the regex_iterator
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数of std::regex_iterator
推进regex_token_iterator
Original:
advances the regex_token_iterator
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数of std::regex_token_iterator

[编辑] 另请参阅

运算符优先级

Common operators
分配 incrementNJdecrement 算术 合乎逻辑的 比较 memberNJaccess 其他

a = b
a = rvalue
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b

++a
--a
a++
a--

+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b

!a
a && b
a || b

a == b
a != b
a < b
a > b
a <= b
a >= b

a[b]
*a
&a
a->b
a.b
a->*b
a.*b

a(...)
a, b
(type) a
? :

Special operators
static_cast将一种类型转换到另一个兼容的类型
Original:
static_cast converts one type to another compatible type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
dynamic_cast将虚基类到派生class
Original:
dynamic_cast converts virtual base class to derived class
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
const_cast转换类型兼容型,与不同的cvqualifiers
Original:
const_cast converts type to compatible type with different cv qualifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
reinterpret_cast转换类型不兼容的type
Original:
reinterpret_cast converts type to incompatible type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
new个分配memory
Original:
new allocates memory
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
delete会释放memory
Original:
delete deallocates memory
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
sizeof查询的大小的type
Original:
sizeof queries the size of a type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
sizeof...查询的大小的参数组(C++11 起)
Original:
sizeof... queries the size of a 参数组 (C++11 起)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typeid查询的类型一个type
信息
Original:
typeid queries the type information of a type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
noexcept进行检查,,如果的表达可以抛出一个异常(C++11 起)
Original:
noexcept checks if an expression can throw an exception (C++11 起)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
alignof查询类型(C++11 起)对齐要求
Original:
alignof queries alignment requirements of a type (C++11 起)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.