Other operators
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
Operator name | Syntax | Overloadable | Prototype examples (for class T) | |
---|---|---|---|---|
Inside class definition | Outside class definition | |||
function call | a(a1, a2)
|
Yes | R T::operator()(Arg1 &a1, Arg2 &a2, ... ...); | N/A |
comma | a, b
|
Yes | T2& T::operator,(T2 &b); | T2& operator,(const T &a, T2 &b); |
conversion | (type) a | Yes | operator type() | N/A |
ternary conditional | a ? b : c
|
No | N/A | N/A |
目录 |
[编辑] 解释
“函数调用”运营商提供的任何对象的语义功能.
原文:
function call operator provides function semantics for any object.
“转换运算符”将定类型转换为另一种类型。作为打算要返回的类型必须是相同的运营商的名称.
原文:
conversion operator converts given type to another type. The name of the operator must be the same as the type intended to be returned.
“三元条件”操作者检查第一个表达式的布尔值,并取代整个运营商条款的第二和第三个表达式,根据所得到的值.
原文:
ternary conditional operator checks the boolean value of the first expression and replaces entire operator clause with the second or the third expression depending on the resulting value.
[编辑] 内置的函数调用操作符
一个函数调用表达式,如E(A1, A2, A3),由命名的功能,
E
,可能为空的表达式列表A1, A2, A3, ...
,括号中的表达式.原文:
A function call expression, such as E(A1, A2, A3), consists of an expression that names the function,
E
, followed by a possibly empty list of expressions A1, A2, A3, ...
, in parentheses.命名函数的表达式,即可
原文:
The expression that names the function can be
a)
左值表达式是指一个函数
原文:
lvalue expression that refers to a function
b)
函数指针
d)
隐含的类成员访问表达式,例如内使用另一个成员函数,成员函数名.
原文:
implicit class member access expression, e.g. member function name used within another member function.
指定的
E
的功能名称(或成员)可以被重载,重载决议规则来决定过载是被称为.原文:
The function (or member) name specified by
E
can be overloaded, 重载决议 rules used to decide which overload is to be called.E
指定的成员函数,它可能是虚拟的,在这种情况下的最终重载该函数将被调用,在运行时动态调度.原文:
If
E
specifies a member function, it may be virtual, in which case the final overrider of that function will be called, using dynamic dispatch at runtime.若要调用该函数,所有的表达式
A1
,A2
,A3
,等,提供作为参数进行评估以任意顺序,和每个函数参数被初始化后,其相应的参数隐式转换如果有必要。如果调用的成员函数,然后转换this指向当前对象的指针,如果通过显式转换this指针所期望的功能。每个参数的初始化和销毁发生在主叫方的情况下,这意味着,例如,如果一个参数的构造函数抛出一个异常,在函数里定义的异常处理程序,甚至作为一个功能的try块,不考虑。如果函数是一个可变参数函数,默认参数促销活动适用于所有参数匹配的省略号参数.原文:
To call the function, all expressions
A1
, A2
, A3
, etc, provided as arguments are evaluated in arbitrary order, and each function parameter is initialized with its corresponding argument after 隐式转换 if neccessary. If the call is made to a member function, then the this pointer to current object is converted as if by explicit cast to the this pointer expected by the function. The initialization and destruction of each parameter occurs in the context of the caller, which means, for example, that if constructor of a parameter throws an exception, the exception handlers defined within the function, even as a function-try block, are not considered. If the function is a variadic function, 默认参数促销活动 are applied to all arguments matched by the ellipsis parameter.一个函数调用表达式的返回值的类型是所选择的函数的返回类型,决定使用静态绑定(忽略了
virtual
)关键字),,即使压倒一切的实际上调用的函数,该函数返回一个不同的类型。这使得压倒一切的函数返回的指针或引用派生的类的基函数返回的返回类型,即C + +支持协变返回类型)。 E
指定了一个析构函数的返回类型是void.原文:
The return type of a function call expression is the return type of the chosen function, decided using static binding (ignoring the
virtual
) keyword), even if the overriding function that's actually called returns a different type. This allows the overriding functions to return pointers or references to classes that are derived from the return type returned by the base function, i.e. C++ supports 协变返回类型). If E
specifies a destructor, the return type is void.类的函数调用表达式的值是左值,如果该函数返回一个lvalue引用或右值引用的功能,是一个值为xValue,如果该函数返回一个右值引用的对象,和是不然一个prvalue的。如果函数的的调用表达式是一个prvalue的对象类型,它必须有完整的类型,但使用时的操作数decltype.
原文:
The value category of a function call expression is lvalue if the function returns an lvalue reference or an rvalue reference to function, is an xvalue if the function returns an rvalue reference to object, and is a prvalue otherwise. If the function call expression is a prvalue of object type, it must have complete type except when used as an operand to decltype.
函数调用表达式语法中的值初始化T()相似,函数样式转换表达T(A1),并指示初始化一个临时T(A1, A2, A3, ...)
T
是名称的类型,其中.原文:
Function call expression is similar in syntax to value initialization T(), to 函数样式转换 expression T(A1), and to direct initialization of a temporary T(A1, A2, A3, ...), where
T
is the name of a type.#include <cstdio> struct S { int f1(double d) { printf("%f \n", d); // variable argument function call } int f2() { f1(7); // member function call, same as this->f1() // integer argument converted to double } }; void f() { puts("function called"); // function call } int main() { f(); // function call S s; s.f2(); // member function call }
输出:
function called 7.000000
[编辑] 内置的逗号操作符
在逗号表达式E1, E2,表达
E1
评价,它的返回值将被丢弃,它的副作用是完成评估的表达E2
开始前(注意,这种能力是失去了与用户定义的operator,
) 原文:
In a comma expression E1, E2, the expression
E1
is evaluated, its return value is discarded, and its side effects are completed before evaluation of the expression E2
begins (note that this ability is lost with user-defined operator,
). 逗号操作符的返回类型和值类是完全相同的返回类型和类的第二个操作数的值,
E2
.原文:
The return type and value category of the comma operator are exactly the return type and the value category of the second operand,
E2
.逗号分隔的列表,如函数的参数列表中的逗号(f(a, b, c)),初始化列表int a[] = {1,2,3}初始化语句(int i, j;)是不是逗号操作符。如果在这种情况下,需要使用逗号操作符,它必须加上括号:f(a, (n++, n+b), c)
原文:
The comma in various comma-separated lists, such as function argument lists (f(a, b, c)), initializer lists int a[] = {1,2,3}, or initialization statements (int i, j;) is not the comma operator. If the comma operator needs to be used in that context, it has to be parenthesized: f(a, (n++, n+b), c)
输出:
n = 2 m = 7
[编辑] 内置的转换运算符
内置的转换运算符(T)expr的表达
expr
进行评估,并进行显式转换的类型T
. 原文:
The built-in conversion operator (T)expr evaluates the expression
expr
and performs explicit cast to the type T
. 的详细描述,请参阅显式转换.
[编辑] 条件运算符
对于每一个对促进算术类型L和R,并为每一种类型P,其中P是一个指针,指针到成员,枚举类型或范围的,下面的函数签名参与重载解析
原文:
For every pair of promoted arithmetic types L and R and for every type P, where P is a pointer, pointer-to-member, or scoped enumeration type, the following function signatures participate in overload resolution:
LR operator?:(bool, L, R ); |
||
T operator?:(bool, T, T ); |
||
其中LR是通常的算术转换
L
和R
执行的结果。不能重载操作符“:”,只存在于这些函数签名的重载决议的目的.原文:
where LR is the result of 通常的算术转换 performed on
L
and R
. The operator “?:” cannot be overloaded, these function signatures only exist for the purpose of overload resolution.条件运算符的第一个操作数是评估和上下文转换bool。完成后的价值评估和副作用的第一个操作数,如果结果为true,第二个操作数进行评估。如果结果false,第三个操作数进行评估.
原文:
The first operand of the conditional operator is evaluated and 上下文转换 to bool. After both the value evaluation and all side effects of the first operand are completed, if the result was true, the second operand is evaluated. If the result was false, the third operand is evaluated.
在有条件的表达E1 ? E2 : E3,下面的规则和限制
原文:
In the conditional expression E1 ? E2 : E3, the following rules and limitations apply:
1)
如果任一
E2
或E3
(或两者)是一个throw表达,条件运算符的结果是其他(未扔)的表达的结果,并且是一个prvalue(后左值到右值,阵列到指针或函数到指针转换)。这样的条件运算符常用的constexpr编程.原文:
If either
E2
or E3
(or both) is a throw-expression, the result of the conditional operator is the result of the other (not throw) expression, and is a prvalue (after lvalue-to-rvalue, array-to-pointer, or function-to-pointer conversion). Such conditional operator is commonly used in constexpr编程.2)
如果两个
E2
或E3
类型void,其结果是一个类型voidprvalue.原文:
If both
E2
or E3
are of type void, the result is a prvalue of type void.3)
E2
E3
有不同的类类型(或同类型的不同的CV资格的)和相同的价值范畴。在这种情况下,试图由一个(并且仅一个)的操作数转换到的其他类型,如下:原文:
E2
and E3
have different class types (or same type with different cv-qualification) and the same value category. In this case, an attempt is made to convert one (and only one) of the operands to the type of the other, as follows:a)
如果他们是左值,左值的引用类型的隐式转换的尝试
原文:
If they are lvalues, an implicit conversion to the lvalue reference type is attempted
b)
如果他们是xvalues,右值引用类型的隐式转换为尝试
原文:
If they are xvalues, an implicit conversion to the rvalue reference type is attempted
c)
如果他们是右值,并具有相同的基类(或一类是一个基类的),操作数(S)的基本类型转换为复制初始化一个临时对象的基本类型.
原文:
If they are rvalues, and have the same base class (or one is a base class of the other), the operand(s) are converted to the base type by copy-initializing a temporary object of the base type.
d)
如果他们是右值,并没有共同的基类,然后试图与另一个操作数的类型的隐式转换.
原文:
If they are rvalues, and have no common base class, then an implicit conversion is attempted to the type of the other operand.
4)
双方
E2
或E3
是相同的类型的glvalues。在这种情况下,结果具有相同的类型和值类.原文:
Both
E2
or E3
are glvalues of the same type. In this case, the result has the same type and value category.5)
如果所有的情况下,上面列出的失败,无论是
E2
或E3
类类型:重载解析尝试选择最好从一种类型转换成其他.原文:
If all cases listed above fail, and either
E2
or E3
has class type: overload resolution is attempted to select the best conversion from one type to the other.6)
如果所有的情况下,上面列出的失败,也不
E2
也不E3
类类型是:第一,左值,右值,数组与指针,函数指针转换应用。然后, 原文:
If all cases listed above fail, and neither
E2
nor E3
have class type: first, lvalue-to-rvalue, array-to-pointer, and function-to-pointer conversions are applied. Then, a)
如果现在
E2
和E3
具有相同的类型,的结果是prvalue的临时的该类型,拷贝初始化的任何操作被选择进行评估后,E1
原文:
if both
E2
and E3
now have the same type, the result is a prvalue temporary of that type, copy-initialized from whatever operand was selected after evaluating E1
b)
E2
E3
算术或枚举型:通常的算术转换,使他们的普通型,这种类型的结果.原文:
E2
and E3
have arithmetic or enumeration type: usual arithmetic conversions are applied to bring them to common type, that type is the result.c)
E2
和E3
是指针或指针和一个空不变,或两个空指针常量,其中之一是一个std::nullptr_t,然后指针转换和资格convrsions适用于常见的类型,该类型的结果.原文:
E2
and E3
are pointers, or a pointer and a null constant, or a both null pointer constants, one of which is a std::nullptr_t, then pointer conversions and qualification convrsions are applied to bring them to common type, that type is the result.d)
E2
E3
是指向成员的指针或指针成员和一个空不变,然后指针成员的转换和资格convrsions的的应用,使他们的普通型,该类型 原文:
E2
and E3
are pointers to members, or a pointer to member and a null constant: then pointer-to-member conversions and qualification convrsions are applied to bring them to common type, that type is the #include <string> #include <stdexcept> struct Node { Node* next; int data; // deep-copying copy constructor Node(const Node& other) : next(other.next ? new Node(*other.next) : NULL) , data(other.data) {} Node(int d) : next(NULL), data(d) {} ~Node() { delete next ; } }; int main() { // simple rvalue example int n = 1>2 ? 10 : 11; // 1>2 is false, so n = 11 // simple lvalue example int m = 10; (n == m ? n : m) = 7; // n == m is false, so m = 7 // throw example std::string str = 2+2==4 ? "ok" : throw std::logic_error("2+2 != 4"); }
[编辑] 标准库
在标准库中的许多类覆盖
operator()
被用来作为函数对象.原文:
Many classes in the standard library override
operator()
to be used as function objects. 删除对象或数组 (公共成员函数of std::default_delete )
| |
业主基于语义的,它的参数进行比较 原文: compares its arguments using owner-based semantics (函数) | |
返回两个参数的总和 (公共成员函数of std::plus )
| |
返回两个参数之间的差异 原文: returns the difference between two arguments (公共成员函数of std::minus )
| |
返回两个参数的产品 原文: returns the product of two arguments (公共成员函数of std::multiplies )
| |
返回的结果的第二个参数,第一个参数的划分 原文: returns the result of the division of the first argument by the second argument (公共成员函数of std::divides )
| |
其余的划分第一个参数的第二个参数返回 原文: returns the remainder from the division of the first argument by the second argument (公共成员函数of std::modulus )
| |
返回否定的说法 原文: returns the negation of the argument (公共成员函数of std::negate )
| |
如果参数的检查都是平等的 (公共成员函数of std::equal_to )
| |
如果参数的检查是不相等的 原文: checks if the arguments are not equal (公共成员函数of std::not_equal_to )
| |
检查如果第一个参数大于第二个 原文: checks if the first argument is greater than the second (公共成员函数of std::greater )
| |
检查第一个参数小于第二个 原文: checks if the first argument is less than the second (公共成员函数of std::less )
| |
第一个参数的检查,如果是大于或等于第二个 原文: checks if the first argument is greater than or equal to the second (公共成员函数of std::greater_equal )
| |
第一个参数的检查,如果小于或等于第二个 原文: checks if the first argument is less than or equal to the second (公共成员函数of std::less_equal )
| |
返回两个参数的逻辑与 原文: returns the logical AND of the two arguments (公共成员函数of std::logical_and )
| |
返回的逻辑或两个参数 原文: returns the logical OR of the two arguments (公共成员函数of std::logical_or )
| |
返回逻辑“非”的说法 原文: returns the logical NOT of the argument (公共成员函数of std::logical_not )
| |
returns the result of bitwise AND of two arguments (公共成员函数of std::bit_and )
| |
返回结果的位或两个参数 原文: returns the result of bitwise OR of two arguments (公共成员函数of std::bit_or )
| |
返回两个参数的按位异或的结果 原文: returns the result of bitwise XOR of two arguments (公共成员函数of std::bit_xor )
| |
返回的逻辑补码的呼叫的结果,所存储的谓词 原文: returns the logical complement of the result of a call to the stored predicate (公共成员函数of std::unary_negate )
| |
返回的逻辑补码的呼叫的结果,所存储的谓词 原文: returns the logical complement of the result of a call to the stored predicate (公共成员函数of std::binary_negate )
| |
调用存储功能 (公共成员函数of std::reference_wrapper )
| |
lexicographically compares two strings using this locale's collate facet (公共成员函数of std::locale )
| |
比较两个值类型 value_type 原文: compares two values of type value_type (公共成员函数of std::map::value_compare )
| |
比较两个值类型 value_type 原文: compares two values of type value_type (公共成员函数of std::multimap::value_compare )
| |
执行相应的功能 (公共成员函数of std::packaged_task )
| |
推进发动机的状态,并返回生成的值 原文: advances the engine's state and returns the generated value (公共成员函数of std::linear_congruential_engine )
| |
生成下一个随机数的分布 原文: generates the next random number in the distribution (公共成员函数of std::uniform_int_distribution )
|
一些标准库中的类提供用户定义的转换功能
原文:
Several standard library classes provide user-defined conversion functions
检查,如果该值是非零的 (公共成员函数of std::error_code )
| |
检查,如果该值是非零的 (公共成员函数of std::error_condition )
| |
访问的bitset的元素 (公共成员函数of std::bitset::reference )
| |
访问的元素的矢量<bool>中 原文: accesses the element of the vector<bool> (公共成员函数of std::vector<bool>::reference )
| |
检查是否有相关的管理对象 原文: checks if there is associated managed object (公共成员函数of std::unique_ptr )
| |
检查是否有相关的管理对象 原文: checks if there is associated managed object (公共成员函数of std::shared_ptr )
| |
访问所存储的参考 (公共成员函数of std::reference_wrapper )
| |
转换的对象 value_type ,返回value 原文: converts the object to value_type , returns value (公共成员函数of std::integral_constant )
| |
(至 C++11) (C++11 起) |
检查,如果没有发生错误(同义词!fail()) 原文: checks if no error has occurred (synonym of !fail()) (公共成员函数of std::basic_ios )
|
转换的基本字符串类型 原文: converts to the underlying string type (公共成员函数of std::sub_match )
| |
从一个原子对象加载的值 原文: loads a value from an atomic object (公共成员函数of std::atomic )
| |
测试的锁是否拥有相关联的互斥 原文: tests whether the lock owns its associated mutex (公共成员函数of std::unique_lock )
| |
托管指针转换到不同类型的指针 原文: converts the managed pointer to a pointer to different type (公共成员函数of std::auto_ptr )
|
逗号操作符重载标准库中的任何一类。 Boost库的使用
operator,
boost.assign,boost.spirit,和其他图书馆.....原文:
The comma operator is not overloaded by any class in the standard library. The boost library uses
operator,
in boost.assign, boost.spirit, and other libraries.[编辑] 另请参阅
Common operators | ||||||
---|---|---|---|---|---|---|
分配 | incrementNJdecrement | 算术 | 合乎逻辑的 | 比较 | memberNJaccess | 其他 |
a = b |
++a |
+a |
!a |
a == b |
a[b] |
a(...) |
Special operators | ||||||