연산자 (프로그래밍): 두 판 사이의 차이

위키백과, 우리 모두의 백과사전.
내용 삭제됨 내용 추가됨
편집 요약 없음
11번째 줄: 11번째 줄:
컴파일러는 [[함수 (프로그래밍)|서브루틴 호출]]이나 [[인라인 확장|인라인 코드]]와 함께 연산자 및 함수 기능을 구현할 수 있다.
컴파일러는 [[함수 (프로그래밍)|서브루틴 호출]]이나 [[인라인 확장|인라인 코드]]와 함께 연산자 및 함수 기능을 구현할 수 있다.


== Operator overloading ==
== 연산자 오버로딩 ==
{{main|Operator overloading}}
{{main|연산자 오버로딩}}


몇몇의 프로그래밍 언어에서 연산자는 아마도 그것은, 자료의 한 종류보다 많은 정의를 가지고 있는 ''애드혹 다형성''일 것이다, ([[자바 (프로그래밍 언어)|자바]]에서 같은 <tt>+</tt> 연산자는 숫자의 덧셈과 문자열의 연결 모두에 사용된다). 이러한 연산자는 ''오버로드된 것''으로 전해지고 있다. 프로그래밍 언어에서 프로그래머가 연산자의 제한된 집합을 가진 연산자 오버로딩을 지원하지만, 연산자 오버로딩은 종종 연산자에 맞게 정의하는 데 사용한다.
In some programming languages an operator may be ''ad-hoc polymorphic'', that is, have definitions for more than one kind of data, (such as in [[Java (programming language)|Java]] where the <tt>+</tt> operator is used both for the addition of numbers and for the concatenation of strings). Such an operator is said to be ''overloaded''. In languages that support operator overloading by the programmer but have a limited set of operators, operator overloading is often used to define customized uses for operators.


== Operand coercion ==
== Operand coercion ==

2011년 9월 14일 (수) 19:59 판

프로그래밍 언어는 일반적으로 수학 연산과 유사한 연산자의 집합을 지원한다. 언어는 내장된 연산자의 정해진 숫자를 포함할 수 있다 (C와 C++에서 + - * = 연산자의 경우), 아니면 프로그래머가 정의한 연산자의 생성을 허용할 수 있다 (하스켈의 경우). 일부 프로그래밍 언어는 다른 div와 같은 이름도 허용하는 동안 특수 문자 + 또는 := 과 같은 연산자 기호를 제한한다 (파스칼의 경우).

언어에 의해 지원된 내장 연산자 일부는 일반적으로 중앙 처리 장치에 있는 명령의 작은 숫자에 직접 매핑을 하지만, 다른 것은 복잡한 구현을 할 수도 있다 (예를 들어, '+'는 문자열 연결을 표현하기 위해 사용한다).

언어의 사양은 우선순위와 그것이 지원하는 연산자의 결합법칙을 지정한다. 프로그래머가 정의된 연산자를 지원하는 언어는 새로운 연산자의 기호 (예: 프롤로그)의 우선순위와 결합법칙의 사양이 필요하다.

대부분의 프로그래밍 언어 연산자는 몇몇 많은 피연산자 (예, C에서 ?: 연산자) 지원과 함께, 하나 또는 두 개의 피연산자를 사용한다. 그것의 피연산자와 관련하여 연산자의 위치는 전위 표기법, 중위 표기법 또는 후위 표기법일 것이다.

구문 연산자는 보통 함수와 대조를 이룬다. 대부분의 언어는, 함수는 아마도 고정된 우선순위 수준과 결합법칙에 대한 전위 연산자의 특별한 형태, 괄호에 대해서 강제적일 것이다. 예: Func(a) (혹은 리스프에서 (Func a)). 대부분의 언어는 프로그래머가 정의한 기능을 지원하지만, 그들은 더 많은 전위 표기법과 더 많은 우선순위 수준이 없으면 프로그래머가 정의 연산자를 지원하도록 실제로 주장할 수 없다. 의미적으로 연산자는 다른 호출 표기법 및 매개 변수의 제한된 숫자 (보통 1 혹은 2개)에 대한 함수의 특별한 형태로 볼 수 있다.

컴파일러는 서브루틴 호출이나 인라인 코드와 함께 연산자 및 함수 기능을 구현할 수 있다.

연산자 오버로딩

몇몇의 프로그래밍 언어에서 연산자는 아마도 그것은, 자료의 한 종류보다 많은 정의를 가지고 있는 애드혹 다형성일 것이다, (자바에서 같은 + 연산자는 숫자의 덧셈과 문자열의 연결 모두에 사용된다). 이러한 연산자는 오버로드된 것으로 전해지고 있다. 프로그래밍 언어에서 프로그래머가 연산자의 제한된 집합을 가진 연산자 오버로딩을 지원하지만, 연산자 오버로딩은 종종 연산자에 맞게 정의하는 데 사용한다.

Operand coercion

Some languages also allow for the operands of an operator to be implicitly converted, or coerced, to suitable data types for the operation to occur. For example, in Perl coercion rules lead into 12 + "3.14" producing the result of 15.14. The text "3.14" is converted to the number 3.14 before addition can take place. Further, 12 is an integer and 3.14 is either a floating or fixed-point number (a number that has a decimal place in it) so the integer is then converted to a floating point or fixed-point number respectively.

Javascript follows opposite rules—finding the same expression above, it will convert the integer 12 into a string "12", then concatenate the two operands to form "123.14".

In the presence of coercions in a language, the programmer must be aware of the specific rules regarding operand types and the operation result type to avoid subtle programming mistakes.

Operator features in programming languages

The following table shows the operator features in several programming languages:

Programming language Nonalphanumeric operator symbols Alphanumeric operator symbols Prefix Infix Postfix Precedence Associativity Overloading Programmer-defined overloading Programmer-defined operator symbols
ALGOL 68 +* ** * / % %* %× - + < <= >= > = /= & -:= +:= *:= /:= %:= %*:= +=: :=: :/=:

(All operators have bold Alphanumeric equivalents, c.f. next column. Some have non ASCII equivalents, c.f. below.) ¬ +× ⊥ ↑ ↓ ⌊ ⌈ × ÷ ÷× ÷* □ ≤ ≥ ≠ ∧ ∨ ×:= ÷:= ÷×:= ÷*:= %×:= :≠:

not abs arg bin entier leng level odd repr round shorten i shl shr up down lwb upb lt le ge gt eq ne and or over mod elem minusab plusab timesab divab overab modab plusto is isnt 아니요 (prefix operators always have priority 10) Infix operators are left associative, prefix operators are right associative
C () [] -> . ! ~ ++ -- + - * & / % << >> < <= > <= == != ^ | && || ?: = += -= *= /= %= &= ^= |= <<= >>= sizeof 아니요 아니요
C++ (more) sizeof new delete throw 아니요
Java new instanceof 아니요 아니요
Pascal * / + - = < > <> <= >= := not div mod and or in 아니요 아니요 아니요
Seed7 {} [] -> ** ! + - * / << >> & >< | = <> > >= < <= <& := +:= -:= *:= /:= <<:= >>:= &:= @:= conv varConv parse conj div rem mdiv mod times mult in not and or digits lpad rpad lpad0
Prolog :- ?- ; , . =.. = \= < =< >= > == \== - + / * spy nospy not is mod 아니요 아니요
Lisp + - * / = /= < <= > >= mod rem floor truncate min max (operator and parameters must be surrounded by parentheses) 아니요 아니요 아니요 아니요 아니요
Smalltalk (yes - Up to two characters[1]) (yes - Needs a colon after the keyword) 아니요 아니요 아니요

See also

References