2014-03-13 3 views
8

binary_function이 C++ 11에서 제거 된 것을 발견했습니다. 이유가 궁금합니다.unary_function을 사용하는 이유는 무엇입니까? binary_function이 C++ 11에서 제거 되었습니까?

C++ 98 :

template <class T> struct less : binary_function <T,T,bool> { 
    bool operator() (const T& x, const T& y) const {return x<y;} 
}; 

C++ 11

template <class T> struct less { 
    bool operator() (const T& x, const T& y) const {return x<y;} 
    typedef T first_argument_type; 
    typedef T second_argument_type; 
    typedef bool result_type; 
}; 

수정 ------------------- -------------------------------------------------- -------

template<class arg,class result> 
struct unary_function 
{ 
     typedef arg argument_type; 
     typedef result result_type; 
}; 

예를 들어 우리가 C + 98는,

template <class T> struct even : unary_function <T,bool> { 
    bool operator() (const T& x) const {return 0==x%2;} 
}; 

find_if(bgn,end,even<int>()); //find even number 

//adapter 
template<typename adaptableFunction > 
class unary_negate 
{ 
    private: 
     adaptableFunction fun_; 
    public: 
     typedef adaptableFunction::argument_type argument_type; 

     typedef adaptableFunction::result_type result_type; 
     unary_negate(const adaptableFunction &f):fun_(f){} 

     bool operator()(const argument_type&x) 
     { 
      return !fun(x); 
     } 
} 

find_if(bgn,end, unary_negate< even<int> >(even<int>())); //find odd number 

우리는 어떻게 unary_function없이 C++ 11이에 향상시킬 수 있습니까? 가변 인자 템플릿

답변

10

제거되지 않았으며 C++ 11에서는 더 이상 사용되지 않습니다. 여전히 C++ 11 표준의 일부입니다. 여전히 자신의 코드에서 사용할 수 있습니다. (편집 : 최근하지만 C++ 17에서 제거 투표위원회.) binary_function에서 파생 구현을 필요로하는 것은 과잉 사양이기 때문에 그것은, 더 이상 표준에 를 사용하지 않는

.

사용자는 단지 그것을 first_argument_type, second_argument_typeresult_type을 정의하는 것을 신경 쓸 필요가, lessbinary_function에서 유래 여부를 신경 쓰지한다. 이러한 typedef를 제공하는 방법은 구현에 달려 있어야합니다.

특정 유형에서 파생되도록 구현을 강요하면 사용자가 파생에 의존하기 시작할 수 있음을 의미합니다. 이는 의미가없고 유용하지 않습니다.

편집

우리는 어떻게 unary_function없이 11 C++에서이를 향상시킬 수 있습니까?

당신은 필요하지 않습니다.

당신이 더 잘 할 수있는 사실
template<typename adaptableFunction> 
class unary_negate 
{ 
    private: 
     adaptableFunction fun_; 
    public: 
     unary_negate(const adaptableFunction& f):fun_(f){} 

     template<typename T> 
      auto operator()(const T& x) -> decltype(!fun_(x)) 
      { 
       return !fun_(x); 
      } 
} 

, not_fn: a generalized negator

12

, 일반 기능 작곡의 많은 너무 오래된 cruft에 모두가 더 이상 필요하지 않습니다, 간단하고 지속적으로 더 많은 표현 될 수있다

수행 사용 :

  • std::function
  • std::bind
  • std::mem_fn
  • std::result_of
  • 람다

는 사용하지 마십시오

  • std::unary_function, std::binary_function
  • std::mem_fun
  • std::bind1ststd::bind2nd
+0

하며 binary_function , 바로 우리의 작업을 단순화하고 코드를보다 일관 할 수 있도록 설정할 수 있습니다? – camino

+1

@camino : 그래도 될까요? 그것없이 –

+0

우리는 형식 정의 T의 first_argument_type이라고 정의해야합니다, ..., 어쩌면 우리는 뭔가 – camino