I have the following template class:
template<class T>
class C
{
typedef C_traits<T> traits;
typedef typename traits::some_type type;
//...
// use 'traits' and 'type' a lot
//...
};
where C_traits is a template struct that has a typedef for some_type that is different for each specialization of the type X. I am trying to write a function that receives a reference to a variable of type as defined above, i.e.:
template<class T>
//void f(const C_traits<T>::some_type& c) <-- this does not work either
void f(const C<T>::type& c)
{
//...
}
I’m getting an “expected unqualified-id before ‘&’ token” error on the line where f is defined. I think I understand why I am getting this error, but I’d like to know if there is any way to accomplish what I’m trying to do here.
Put another way, I’d like to do something like this:
template<class T>
void f(typename const C<T>::type& c)
{
//...
}
which is not allowed. Any thoughs?
This is allowed, but a qualified type is one of the non-deduced contexts. That is, template argument deduction won’t work for this, so you’ll have to call the function like so:
If you don’t want to supply the type argument explicitly, one option is to have the function just take a
T: