Namespaces
Variants

C++ named requirements: FunctionObject

From cppreference.net
C++ named requirements

A FunctionObject 타입은 함수 호출 연산자 왼쪽에 사용될 수 있는 객체의 타입입니다.

목차

요구사항

타입 T 가 다음 조건을 만족하면 FunctionObject 를 만족합니다

주어진

  • f , T 타입 또는 const T 타입의 값,
  • args , 비어 있을 수 있는 적절한 인수 목록.

다음 표현식들은 유효해야 합니다:

Expression Requirements
f ( args ) 함수 호출을 수행함

참고 사항

함수와 함수에 대한 참조는 함수 객체 타입이 아니지만, 함수-대-포인터 암시적 변환 으로 인해 함수 객체 타입이 기대되는 곳에서 사용될 수 있습니다.

표준 라이브러리

예제

다양한 종류의 함수 객체를 보여줍니다.

#include <functional>
#include <iostream>
void foo(int x) { std::cout << "foo(" << x << ")\n"; }
void bar(int x) { std::cout << "bar(" << x << ")\n"; }
int main()
{
    void(*fp)(int) = foo;
    fp(1); // 함수 포인터를 사용하여 foo 호출
    std::invoke(fp, 2); // 모든 FunctionObject 타입은 Callable입니다
    auto fn = std::function(foo); // <functional>의 나머지 부분도 참조하세요
    fn(3);
    fn.operator()(3); // fn(3)과 동일한 효과
    struct S
    {
        void operator()(int x) const { std::cout << "S::operator(" << x << ")\n"; }
    } s;
    s(4); // s.operator() 호출
    s.operator()(4); // s(4)와 동일
    auto lam = [](int x) { std::cout << "lambda(" << x << ")\n"; };
    lam(5); // 람다 호출
    lam.operator()(5); // lam(5)와 동일
    struct T
    {
        using FP = void (*)(int);
        operator FP() const { return bar; }
    } t;
    t(6); // t는 함수 포인터로 변환됨
    static_cast<void (*)(int)>(t)(6); // t(6)과 동일
    t.operator T::FP()(6); // t(6)과 동일
}

출력:

foo(1)
foo(2)
foo(3)
foo(3)
S::operator(4)
S::operator(4)
lambda(5)
lambda(5)
bar(6)
bar(6)
bar(6)

참고 항목

호출 연산이 정의된 타입
(명명된 요구 사항)