는 다음 사항을 고려 회원의 메소드 호출템플릿 전달은
당신이 상상할 수있는,has_my_method_callable_with_the_following_types
가
type
가 호출 할 수있는 방법이있는 경우 나 확인할 수 있습니다 SFINAE 구조체의 일종이다,
template <typename type> class my_wrapper
{
type _;
template <typename... types, typename std :: enable_if <has_my_method_callable_with_the_following_types <type, types...> :: value> :: type * = nullptr> void my_method(types... items)
{
_.my_method(items...);
}
};
그 유형들.
쉽게 알 수 있듯이 위의 예는 기본적으로 my_method
에 대한 모든 호출을 _
으로 전달합니다. 잘. 거의 모두입니다. my_type :: my_method
참조로 그것을 수용하면서 x
복사하여 함수에 전달 될 것처럼
class my_type
{
void my_method(int & x)
{
x++;
}
};
my_wrapper <my_type> my_wrapped;
int x;
my_wrapped.my_method(x);
은 분명히 위, 작동하지 않습니다 : 어떻게 내가 할 경우 발생합니다. 그래서 나는 궁금하다.이 문제를 해결할 방법이 있을까?
template <typename... types, typename std :: enable_if <has_my_method_callable_with_the_following_types <type, types & ...> :: value> :: type * = nullptr> void my_method(types & ... items)
{
_.my_method(items...);
}
을하지만 대칭 나는 말, 전달하고 문제가, 내가 참조하지 수 있지만 일부 my_type :: my_method(int x)
에 의해 완벽하게 허용 될 수 int
리터럴을해야합니다 : 물론 나는 할 수있다.
이 문제를 해결하려면 어떻게해야합니까? my_wrapper <type> :: my_method
에 대한 모든 통화를 type :: my_method
으로 원활하게 전달하고 싶습니다.
해적주의 : 나는 상속을 사용할 수 없으므로 제안하지 마십시오!
이T
를 추론 할 때 말한다 언어에 특별한 규칙이있다 :이 어떻게 작동
template <
typename... types,
typename std :: enable_if <has_my_method_callable_with_the_following_types <type, types...> :: value> :: type * = nullptr
> void my_method(types&&... items)
{
_.my_method(std::forward<types>(items)...);
}
: :)
'완벽한 전달'을 찾고 있습니다 – lorro