silly question regarding template

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
 
I

Ian Collins

toton said:
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> template <typename f_iter>
void Test<T>::assign(f_iter from,f_iter to){...
 
T

toton

Ian said:
toton said:
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> template <typename f_iter>
void Test<T>::assign(f_iter from,f_iter to){...

Thanks. That is the first thing I had tried. But it was giving some
other error like multiple template definition! .So I thought writing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,829
Latest member
KimberAlli

Latest Threads

Top