내가 형 무효 (* FUNC) (T)와 FUNC의 인수 인수의 기능을 소요 펑터 F를 쓰고에서 const를 또는 const가 아닌 참조 형을 얻는다.는 특성
template<typename T>
void F(void (*func)(T), WhatTypeHere? arg)
{
func(arg);
}
그러면 functor F는 arg로 func를 호출합니다. 나는 F를 arg를 복사하지 말고 그냥 참조로 전달하기를 바란다. 그러나 나는 간단하기 때문에 T는 참조가 될 수있다 "(* FUNC) (T), T & (무효) 무효 F"를 쓸 수 없습니다.
T -> T&
T& -> T&
const T -> const T&
const T& -> const T&
나는 이런 식으로 뭔가 들고 오지 : 그래서 T의 적절한 참조 유형을 얻을 수있는 특성을 쓰기 위해 노력하고 있어요
에 대한 예를 들어 작동하지 않습니다template<typename T>
struct type_op
{
typedef T& valid_ref_type;
};
template<typename T>
struct type_op<T&>
{
typedef typename type_op<T>::valid_ref_type valid_ref_type;
};
template<typename T>
struct type_op<const T>
{
typedef const T& valid_ref_type;
};
template<typename T>
struct type_op<const T&>
{
typedef const T& valid_ref_type;
};
template<typename T>
void F(void (*func)(T), typename type_op<T>::valid_ref_type arg)
{
func(arg);
}
void a(int x) { std::cout << x << std::endl; }
F(&a, 7);
주기 오류 : 인수 2를 통과 입력 유형 'INT'의 임시에서 '&을 int 형'의 const가 아닌 참조 무효 초기화 의 '무효 F (무효 (*) (T), 유형 이름 type_op :: valid_ref_type)와에게 T는 = INT]'
어떻게이 특성은 일하러 가야? 그런 다음 다음과 같이template<typename T> struct remove_reference { typedef T type; };
template<typename T> struct remove_reference<T&> { typedef T type; };
다시 추가 :
는 는 는다음과 같이 함수를 선언해야한다
remove_reference<T>::type&
:
template<typename T>
void F(void (*func)(T), const typename remove_reference<T>::type& arg)
{
func(arg);
}
나는 이것과 꽤 비슷하다고 생각한다. http://stackoverflow.com/questions/2539361/c-template-functor-cannot-deduce-reference-type –