T
toton
Hi,
This is a silly question related to syntax only.
I have a template class with template member function, How to write it
separately in a file (not translation unit, just want to separate
declaration from definition! ).
e.g, for ordinary template class member function it is done like,
template <typename T>
class Test{
public:
typedef T value_type;
typedef const T& const_reference;
typedef T& reference;
typedef size_t size_type;
const_reference get(size_type i) const;
reference get(size_type i){
return array_;
}
template<typename f_iter>
void assign(f_iter from,f_iter to){
size_type i = 0;
while(from!=to){
array_[i%10] = *from;
++i; ++from;
}
}
private:
T array_[10];
};
Now If I want get to implement separately, I use
template <typename T>
typename Test<T>::reference Test<T>::get(size_type i){
return array_;
}
Now how to write the assign template member function separately,
Of course this is now working. What is the proper syntax?
template <typename T>
void Test<T>::assign(f_iter from,f_iter to){
typename Test<T>::size_type i = 0;
while(from!=to){
array_[i%10] = *from;
++i; ++from;
}
}
(I am aware that Test class don't do anything usefull. However it
compiles, and (hopefully) has no memory leak and has exception safety.
I am using it for demonstration purpose only. I just want to know the
syntax.
Thanks
This is a silly question related to syntax only.
I have a template class with template member function, How to write it
separately in a file (not translation unit, just want to separate
declaration from definition! ).
e.g, for ordinary template class member function it is done like,
template <typename T>
class Test{
public:
typedef T value_type;
typedef const T& const_reference;
typedef T& reference;
typedef size_t size_type;
const_reference get(size_type i) const;
reference get(size_type i){
return array_;
}
template<typename f_iter>
void assign(f_iter from,f_iter to){
size_type i = 0;
while(from!=to){
array_[i%10] = *from;
++i; ++from;
}
}
private:
T array_[10];
};
Now If I want get to implement separately, I use
template <typename T>
typename Test<T>::reference Test<T>::get(size_type i){
return array_;
}
Now how to write the assign template member function separately,
Of course this is now working. What is the proper syntax?
template <typename T>
void Test<T>::assign(f_iter from,f_iter to){
typename Test<T>::size_type i = 0;
while(from!=to){
array_[i%10] = *from;
++i; ++from;
}
}
(I am aware that Test class don't do anything usefull. However it
compiles, and (hopefully) has no memory leak and has exception safety.
I am using it for demonstration purpose only. I just want to know the
syntax.
Thanks