Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Other operators

      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
       
      Expressions
      General
      Literals
      Operators
      Conversions
       
      Operator
      name
      SyntaxOver​load​ablePrototype examples (forclass T)
      Inside class definitionOutside class definition
      function calla(a1, a2)YesR T::operator()(Arg1&a1, Arg2&a2, ...);N/A
      commaa, bYesT2& T::operator,(T2&b);T2& operator,(const T&a, T2&b);
      conditional operatora ? b : c NoN/AN/A

      Thefunction call operator provides function semantics for any object.

      Theconditional operator (colloquially referred to asternary conditional ) checks the boolean value of the first expression and, depending on the resulting value, evaluates and returns either the second or the third expression.

      Contents

      [edit]Built-in function call operator

      Function call expressions have the following form:

      function (arg1,arg2,arg3,...)
      function - an expression function type or function pointer type
      arg1,arg2,arg3,... - a possibly empty list of arbitrary expressions orbrace-enclosed initializer lists(since C++11), except the comma operator is not allowed at the top level to avoid ambiguity

      For a call to a non-member function or to astatic member function,function can be an lvalue that refers to a function (in which case thefunction-to-pointer conversion is suppressed), or a prvalue of function pointer type.

      The function (or member) name specified byfunction can be overloaded,overload resolution rules used to decide which overload is to be called.

      Iffunction 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.

      Each function parameter is initialized with its corresponding argument afterimplicit conversion if necessary.

      • If there is no corresponding argument, the correspondingdefault argument is used, and if there is none, the program is ill-formed.
      • If the call is made to a member function, then thethis pointer to current object is converted as if by explicit cast to thethis pointer expected by the function.
      • The initialization and destruction of each parameter occurs in the context of thefull-expression where the function call appears, which means, for example, that if a constructor or destructor of a parameter throws an exception, thefunctiontry blocks of the called function are not considered.

      If the function is a variadic function,default argument promotions are applied to all arguments matched by the ellipsis parameter.

      It is implementation-defined whether a parameter is destroyed when the function in which it is defined exits or at the end of the enclosing full-expression. Parameters are always destroyed in the reverse order of their construction.

      The return type of a function call expression is the return type of the chosen function, decided using static binding (ignoring thevirtual keyword), even if the overriding function that is 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++ supportscovariant return types. Iffunction specifies a destructor, the return type isvoid.

      When an object of class typeX is passed to or returned from a function, if each copy constructor, move constructor, and destructor ofX is either trivial or deleted, andX has at least one non-deleted copy or move constructor, implementations are permitted to create a temporary object to hold the function parameter or result object.

      The temporary object is constructed from the function argument or return value, respectively, and the function's parameter or return object is initialized as if by using the non-deleted trivial constructor to copy the temporary (even if that constructor is inaccessible or would not be selected by overload resolution to perform a copy or move of the object).

      This allows objects of small class types, such asstd::complex orstd::span, to be passed to or returned from functions in registers.

      (since C++17)

      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 havecomplete type except when used as the operand ofdecltype (or as the right operand of abuilt-in comma operator that is the operand ofdecltype)(since C++11).

      When the called function exits normally, allpostcondition assertions of the function areevaluated in sequence. If the implementation introduces anytemporary objects to hold the result value, for the evaluationE of each postcondition assertion:

      (since C++26)

      Function call expression is similar in syntax to value initializationT(), tofunction-style cast expressionT(A1), and to direct initialization of a temporaryT(A1, A2, A3, ...), whereT is the name of a type.

      Run this code
      #include <cstdio> struct S{int f1(double d){return printf("%f\n", d);// variable argument function call} int f2(){return 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}

      Output:

      function called7.000000

      [edit]Built-in comma operator

      Comma expressions have the following form:

      E1,E2

      In a comma expressionE1, E2, the expressionE1 is evaluated, its result isdiscarded (although if it has class type, it won't be destroyeduntil the end of the containing full expression), and its side effects are completed before evaluation of the expressionE2 begins(note that a user-definedoperator, cannot guarantee sequencing)(until C++17).

      The type, value, and value category of the result of the comma expression are exactly the type, value, and value category of the second operand,E2. IfE2 is a temporaryexpression(since C++17), the result of the expression is that temporaryexpression(since C++17). IfE2 is a bit-field, the result is a bit-field.

      The comma in various comma-separated lists, such as function argument lists (f(a, b, c)) and initializer listsint a[]={1,2,3}, is not the comma operator. If the comma operator needs to be used in such contexts, it has to be parenthesized:f(a,(n++, n+ b), c).

      Using an unparenthesized comma expression as second (right) argument of asubscript operator is deprecated.

      For example,a[b, c] is deprecated anda[(b, c)] is not.

      (since C++20)
      (until C++23)

      An unparenthesized comma expression cannot be second (right) argument of asubscript operator. For example,a[b, c] is either ill-formed or equivalent toa.operator[](b, c).

      Parentheses are needed when using a comma expression as the subscript, e.g.,a[(b, c)].

      (since C++23)
      Run this code
      #include <iostream> int main(){// comma is often used to execute more than one expression// where the language grammar allows only one expression: // * in the third component of the for loopfor(int i=0, j=10; i<= j;++i,--j)//            ^list separator      ^comma operatorstd::cout<<"i = "<< i<<" j = "<< j<<'\n'; // * in a return statement// return log("an error!"), -1; // * in an initializer expression// MyClass(const Arg& arg)// : member{ throws_if_bad(arg), arg } // etc. // comma operators can be chained; the result of the last// (rightmost) expression is the result of the whole chain:int n=1;int m=(++n,std::cout<<"n = "<< n<<'\n',++n,2* n); // m is now 6std::cout<<"m = "<<(++m, m)<<'\n';}

      Output:

      i = 0 j = 10i = 1 j = 9i = 2 j = 8i = 3 j = 7i = 4 j = 6i = 5 j = 5n = 2m = 7

      [edit]Conditional operator

      The conditional operator expressions have the form

      E1?E2:E3

      E1 is evaluated andcontextually converted tobool, if the result istrue, the result of the conditional expression is the value ofE2 ; otherwise the result of the conditional expression is the value ofE3 .

      The type and value category of the conditional expressionE1? E2: E3 are determined as follows:

      [edit]Stage 1

      If bothE2 andE3 are of typevoid, the result isan rvalue(until C++11)a prvalue(since C++11) of typevoid.

      If exactly one ofE2 andE3 is of typevoid:

      • If that operand of typevoid is be a (possibly parenthesized)throw expression, the result has the type and the value category of the other operand[1]. If the other operand is abit-field, the result is also a bit-field.
      • Otherwise, the program is ill-formed.

      If neither ofE2 andE3 is of typevoid, proceed to the next stage.

      2+2==4?throw123:throw456;// the result is of type “void” 2+2!=4?"OK":throw"error";// the result is of type “const char[3]”// even if an exception is always thrown

      [edit]Stage 2

      IfE2 orE3 arelvalue bit-fields(until C++11)glvalue bit-fields of the same value category(since C++11) and of typescv1T andcv2T, respectively, the operands are considered to be of typecvT for the remaining process, wherecv is the union ofcv1 andcv2.

      IfE2 andE3 have different types, and any of the following conditions is satisfied, proceed to stage 3:

      • At least one ofE2 andE3 is a (possibly cv-qualified) class type.
      • Both ofE2 andE3 arelvalues of the same type(until C++11)glvalues of the same value category and the same type(since C++11) except for cv-qualification.

      Otherwise, proceed to stage 4.

      [edit]Stage 3

      Attempts are made to form animplicit conversion sequence[2] from an operand expressionX of typeTX to atarget type related to the typeTY of the operand expressionY as follows:

      • IfY is an lvalue, the target type isTY&, but an implicit conversion sequence can only be formed if the reference wouldbind directly toan lvalue(until C++11)a glvalue(since C++11).
      • IfY is an xvalue, the target type isTY&&, but an implicit conversion sequence can only be formed if the reference would bind directly.
      (since C++11)
      • IfY isan rvalue(until C++11)a prvalue(since C++11) or if none of the conversion sequences above can be formed, and at least one ofTX andTY is a (possibly cv-qualified) class type:
        • IfTX andTY are the same class type (ignoring cv-qualification):
          • IfTY is at least as cv-qualified asTX, the target type isTY.
          • Otherwise, no conversion sequence is formed.
        • Otherwise, ifTY is a base class ofTX, the target type isTY with the cv-qualifiers ofTX.
        • Otherwise, the target type is the type ofZ, whereZ is the value ofY after applying the lvalue-to-rvalue, array-to-pointer, and function-to-pointerstandard conversions.
      • Otherwise, no conversion sequence is formed.

      Using this process, it is determined whether an implicit conversion sequence can be formed fromE2 to the target type determined for theE3, and vice versa.

      • If no conversion sequence can be formed, proceed to the next stage.
      • If exactly one conversion sequence can be formed:
        • If the conversion sequence is ambiguous, the program is ill-formed.
        • Otherwise, that conversion is applied to the chosen operand and the converted operand is used in place of the original operand for the remaining process, and proceed to the next stage.
      • If both sequences can be formed, the program is ill-formed.
      struct A{}; struct B: A{}; using T=const B; A a=true? A(): T();// Y = A(), TY = A, X = T(), TX = const B, Target = const A

      [edit]Stage 4

      IfE2 andE3 are lvalues of the same type, then the result is an lvalue of that type, and is a bit-field if at least one ofE2 andE3 is a bit-field.

      (until C++11)

      IfE2 andE3 are glvalues of the same type and the same value category, then the result has the same type and value category, and is a bit-field if at least one ofE2 andE3 is a bit-field.

      (since C++11)

      Otherwise, the result isan rvalue(until C++11)a prvalue(since C++11).

      • IfE2 andE3 do not have the same type, and either has (possibly cv-qualified) class type, proceed to stage 5.
      • Otherwise, proceed to stage 6.

      [edit]Stage 5

      Overload resolution is performed using thebuilt-in candidates to attempt to convert the operands to built-in types:

      • If the overload resolution fails, the program is ill-formed.
      • Otherwise, the selected conversions are applied and the converted operands are used in place of the original operands for the remaining process. Proceed to the next stage.

      [edit]Stage 6

      The array-to-pointer and function-to-pointer conversions are applied to (possibly-converted)E2 andE3. After those conversions, at least one of the following conditions must hold, otherwise the program is ill-formed:

      • E2 andE3 have the same type. In this case, the result is of that type and the result iscopy-initialized using the selected operand.
      • BothE2 andE3 have arithmetic or enumeration type. In this case,usual arithmetic conversions are applied to bring them to their common type, and the result is of that type.
      • At least one ofE2 andE3 is a pointer. In this case, lvalue-to-rvalue, pointer, function pointer(since C++17) and qualification conversions are applied to bring them to theircomposite pointer type, and the result is of that type.
      • At least one ofE2 andE3 is a pointer to member. In this case, lvalue-to-rvalue, pointer-to-member, function pointer(since C++17) and qualification conversions are applied to bring them to theircomposite pointer type, and the result is of that type.
      • BothE2 andE3 are null pointer constants, and at least one of which is of typestd::nullptr_t. In this case, the result is of typestd::nullptr_t.
      (since C++11)
      int* intPtr; using Mixed= decltype(true? nullptr: intPtr); static_assert(std::is_same_v<Mixed,int*>);// nullptr becoming int* struct A{int* m_ptr;} a; int* A::* memPtr=&A::m_ptr;// memPtr is a pointer to member m_ptr of A // memPtr makes nullptr as type of pointer to member m_ptr of Astatic_assert(std::is_same_v<decltype(false? memPtr: nullptr),int*A::*>); // a.*memPtr is now just pointer to int and nullptr also becomes pointer to intstatic_assert(std::is_same_v<decltype(false? a.*memPtr: nullptr),int*>);
      1. Such conditional operator was commonly used in C++11constexpr programming prior to C++14.
      2. Member access, whether a conversion function is deleted(since C++11) and whether an operand is a bit-field are ignored.

      The result type of a conditional operator is also accessible as the binary type traitstd::common_type.

      (since C++11)

      [edit]Overloads

      For every pair of promoted arithmetic typesL andR and for every typeP, whereP is a pointer, pointer-to-member, or scoped enumeration type, the following function signatures participate in overload resolution:

      LR operator?:(bool, L, R);
      P operator?:(bool, P, P);

      where LR is the result ofusual arithmetic conversions performed onL andR.

      The operator “?:” cannot be overloaded, these function signatures only exist for the purpose of overload resolution.

      Run this code
      #include <iostream>#include <string> 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 exampleint n=1>2?10:11;// 1 > 2 is false, so n = 11 // simple lvalue exampleint m=10;(n== m? n: m)=7;// n == m is false, so m = 7 //output the resultstd::cout<<"n = "<< n<<"\nm = "<< m;}

      Output:

      n = 11m = 7

      [edit]Standard library

      Many classes in the standard library overloadoperator() to be used as function objects.

      deletes the object or array
      (public member function ofstd::default_delete<T>)[edit]
      returns the sum of two arguments
      (public member function ofstd::plus<T>)[edit]
      returns the difference between two arguments
      (public member function ofstd::minus<T>)[edit]
      returns the product of two arguments
      (public member function ofstd::multiplies<T>)[edit]
      returns the result of the division of the first argument by the second argument
      (public member function ofstd::divides<T>)[edit]
      returns the remainder from the division of the first argument by the second argument
      (public member function ofstd::modulus<T>)[edit]
      returns the negation of the argument
      (public member function ofstd::negate<T>)[edit]
      checks if the arguments are equal
      (public member function ofstd::equal_to<T>)[edit]
      checks if the arguments are not equal
      (public member function ofstd::not_equal_to<T>)[edit]
      checks if the first argument is greater than the second
      (public member function ofstd::greater<T>)[edit]
      checks if the first argument is less than the second
      (public member function ofstd::less<T>)[edit]
      checks if the first argument is greater than or equal to the second
      (public member function ofstd::greater_equal<T>)[edit]
      checks if the first argument is less than or equal to the second
      (public member function ofstd::less_equal<T>)[edit]
      returns the logical AND of the two arguments
      (public member function ofstd::logical_and<T>)[edit]
      returns the logical OR of the two arguments
      (public member function ofstd::logical_or<T>)[edit]
      returns the logical NOT of the argument
      (public member function ofstd::logical_not<T>)[edit]
      returns the result of bitwise AND of two arguments
      (public member function ofstd::bit_and<T>)[edit]
      returns the result of bitwise OR of two arguments
      (public member function ofstd::bit_or<T>)[edit]
      returns the result of bitwise XOR of two arguments
      (public member function ofstd::bit_xor<T>)[edit]
      returns the logical complement of the result of a call to the stored predicate
      (public member function ofstd::unary_negate<Predicate>)[edit]
      returns the logical complement of the result of a call to the stored predicate
      (public member function ofstd::binary_negate<Predicate>)[edit]
      calls the stored function
      (public member function ofstd::reference_wrapper<T>)[edit]
      invokes the target
      (public member function ofstd::function<R(Args...)>)[edit]
      invokes the target
      (public member function ofstd::move_only_function)[edit]
      invokes the target
      (public member function ofstd::copyable_function)[edit]
      resumes execution of the coroutine
      (public member function ofstd::coroutine_handle<Promise>)[edit]
      lexicographically compares two strings using this locale's collate facet
      (public member function ofstd::locale)[edit]
      compares two values of typevalue_type
      (public member function ofstd::map<Key,T,Compare,Allocator>::value_compare)[edit]
      compares two values of typevalue_type
      (public member function ofstd::multimap<Key,T,Compare,Allocator>::value_compare)[edit]
      executes the function
      (public member function ofstd::packaged_task<R(Args...)>)[edit]
      advances the engine's state and returns the generated value
      (public member function ofstd::linear_congruential_engine<UIntType,a,c,m>)[edit]
      (C++11)
      generates the next random number in the distribution
      (public member function ofstd::uniform_int_distribution<IntType>)[edit]

      The comma operator is not overloaded by any class in the standard library. The boost library usesoperator, inboost.assign,boost.spirit, and other libraries. The database access librarySOCI also overloadsoperator,.

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      CWG 446C++98it was unspecified whether a temporary is created for an
      lvalue-to-rvalue conversion on the conditional operator
      always creates a temporary if
      the operator returns a class rvalue
      CWG 462C++98if the second operand of a comma operator is a temporary,
      it was unspecified whether its lifetime will be extended when
      the result of the comma expression is bound to a reference
      the result of the comma expression
      is the temporary in this case
      (hence its lifetime is extended)
      CWG 587C++98when the second and third operands of a conditional
      operator are lvalues of the same type except for
      cv-qualification, the result was an lvalue if these
      operands have class types or an rvalue otherwise
      the result is always
      an lvalue in this case
      CWG 1029C++98the type of a destructor call was unspecifiedspecified asvoid
      CWG 1550C++98parenthesizedthrow expressions were not allowed in
      conditional expressions if other operand is non-void
      accepted
      CWG 1560C++98void operand of conditional operators caused
      gratuitous lvalue-to-rvalue conversion on the
      other operand, always resulting in rvalue
      a conditional expression
      with avoid can be lvalue
      CWG 1642C++98the expressionfunction in a function call
      expression could be a function pointer lvalue
      not allowed
      CWG 1805C++98when determining the target type for the implicit conversion
      sequence, the way to convertY toZ was unclear
      made clear
      CWG 1895C++98
      C++11
      unclear if deleted (C++11) or inaccessible (C++98)
      conversion function prevents conversion in
      conditional expressions, and conversions from base
      class to derived class prvalue were not considered
      handled like
      overload resolution
      CWG 1932C++98same-type bit-fields were missing in conditional expressionshandled by underlying types
      CWG 2226C++11when determining the target type of the other
      operand of a conditional operator, reference could
      not bind to an xvalue if that operand is an lvalue
      allowed
      CWG 2283C++17the type completeness requirement for function call
      operator was accidently removed byP0135R1
      restored the requirement
      CWG 2321C++98when determining the target type of the other operand
      of a conditional operator, a derived class type could
      not be converted to a less cv-qualified base class type
      allowed to convert to the base
      class type with the cv-qualification
      from the derived class operand
      CWG 2715C++98the initialization and destruction of each
      parameter would occur within the context of
      the calling function, which might not exist[1]
      occurs within the context of
      the enclosing full-expression
      CWG 2850C++98the destruction order of parameters was unclearmade clear
      CWG 2865C++98ifTX andTY are the same class type andTX is
      more cv-qualified thanTY, an implicit conversion
      sequence could still be formed from a prvalueY
      no conversion sequence
      will be formed in this case
      CWG 2906C++98lvalue-to-rvalue conversions were unconditionally applied
      in the rvalue result case for the conditional operator
      only applied in some cases
      1. For example, functions can be called in the initializer of a namespace-scope variable, there is no “calling function” in this context.

      [edit]See also

      Operator precedence
      Operator overloading

      Common operators
      assignmentincrement
      decrement
      arithmeticlogicalcomparisonmember
      access
      other

      a= b
      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
      a->b
      a.b
      a->*b
      a.*b

      function call

      a(...)
      comma

      a, b
      conditional

      a? b: c
      Special operators

      static_cast converts one type to another related type
      dynamic_cast converts within inheritance hierarchies
      const_cast adds or removescv-qualifiers
      reinterpret_cast converts type to unrelated type
      C-style cast converts one type to another by a mix ofstatic_cast,const_cast, andreinterpret_cast
      new creates objects with dynamic storage duration
      delete destructs objects previously created by the new expression and releases obtained memory area
      sizeof queries the size of a type
      sizeof... queries the size of apack(since C++11)
      typeid queries the type information of a type
      noexcept checks if an expression can throw an exception(since C++11)
      alignof queries alignment requirements of a type(since C++11)

      C documentation forOther operators
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/operator_other&oldid=181370"

      [8]ページ先頭

      ©2009-2025 Movatter.jp