By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,442 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

silly question regarding template

P: n/a
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_[i];
}
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_[i];
}

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

Oct 6 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
toton wrote:
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_[i];
}
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_[i];
}

Now how to write the assign template member function separately,
Of course this is now working. What is the proper syntax?
template <typename Ttemplate <typename f_iter>
void Test<T>::assign(f_iter from,f_iter to){...

--
Ian Collins.
Oct 6 '06 #2

P: n/a

Ian Collins wrote:
toton wrote:
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_[i];
}
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_[i];
}

Now how to write the assign template member function separately,
Of course this is now working. What is the proper syntax?

template <typename Ttemplate <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
template twice is not permitted! I suppose I forgot Test<T>:: part,
and was unable to decode the debug message. Now it works fine.
Ian Collins.
Oct 6 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.