문제

I am writing a functor F which takes function of type void (*func)(T) and func's argument arg.

template<typename T>
  void F(void (*func)(T), WhatTypeHere? arg)
{
  func(arg);
}

Then functor F calls func with arg. I would like F not to copy arg, just to pass it as reference. But then I cannot simply write "void F(void (*func)(T), T&)" because T could be a reference. So I am trying to write a trait, which allows to get proper reference type of T:

T -> T&
T& -> T&
const T -> const T&
const T& -> const T&

I come up with something like this:

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);
}

Which doesn't work for example for

void a(int x) { std::cout << x << std::endl; }
F(&a, 7);

Giving error: invalid initialization of non-const reference of type ‘int&’ from a temporary of type ‘int’ in passing argument 2 of ‘void F(void (*)(T), typename type_op::valid_ref_type) [with T = int]’

How to get this trait to work?

도움이 되었습니까?

해결책

template<class T>
struct forwarding { typedef T const& type; };
template<class T>
struct forwarding<T&> { typedef T& type; };

template<typename T>
void F(void (*func)(T), typename forwarding<T>::type arg) {
  func(arg);
}

void a(int x) { std::cout << x << std::endl; }
int main() {
  F(&a, 7);
}

Your mapping was close, you actually want T mapped to T const& too:

T        -> T const&
T&       -> T&
T const& -> T const&

Note that functions having a parameter type of T const have a signature of T! The const is an implementation detail:

void f(int const);
typedef void F(int); // typedef of function type
F* p = &f; // no error! f's signature doesn't include const

다른 팁

All you need is to remove a reference:

template<typename T> struct remove_reference { typedef T type; };
template<typename T> struct remove_reference<T&> { typedef T type; };

Then add it again as follows:

remove_reference<T>::type&

Your function should be declared as follows:

template<typename T>
void F( void (*func)(T), const typename remove_reference<T>::type& arg )
{
  func(arg);
}

It's a bit vague in my mind, but I think that boost (maybe boost::bind) solves this by only providing const T& traits, and requiring the use of ref(x) to indicate a non-const reference.

You could also use add_reference from Boost.TypeTraits to achieve the type-mapping you need.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top