Reputation: 73
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?
Upvotes: 3
Views: 2533
Reputation: 12942
You could also use add_reference
from Boost.TypeTraits to achieve the type-mapping you need.
Upvotes: 1
Reputation:
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
Upvotes: 5
Reputation: 99685
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);
}
Upvotes: 2
Reputation: 186078
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.
Upvotes: 1