446,148 Members | 1,363 Online
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

# Problem on template definition

 P: n/a Dear All; Would you please help me to look at the following case: //! Rotation. enum Rotation { NON_CYCLIC, CYCLIC }; //! Rotation. enum Direction { LEFT, RIGHT }; //! This is a class template of TinyVector template class TinyVector { public: //! Rotate. template TinyVector& Rotate(size_t n); } //! Rotate. template template inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(size_t n) { ... return *this; } //! Rotate. template template<> inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(size_t n) { ... return *this; } // <-- Error position Error C2768: 'TinyVector<_T,_n>::Rotate' : illegal use of explicit template arguments I realy appreciate your guys' help. It make me to learn c++ much more comforably. Shuisheng Sep 27 '06 #1
7 Replies

 P: n/a shuisheng wrote: Dear All; Would you please help me to look at the following case: //! Rotation. enum Rotation { NON_CYCLIC, CYCLIC }; //! Rotation. enum Direction { LEFT, RIGHT }; //! This is a class template of TinyVector template class TinyVector { public: //! Rotate. template TinyVector& Rotate(size_t n); } //! Rotate. template template inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(size_t n) { ... return *this; } //! Rotate. template template<> Reverse those two template declaration. It becomes okay. Quite interesting. template<> template inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(size_t n) { ... return *this; } // <-- Error position Error C2768: 'TinyVector<_T,_n>::Rotate' : illegal use of explicit template arguments I realy appreciate your guys' help. It make me to learn c++ much more comforably. Shuisheng Sep 27 '06 #2

 P: n/a shuisheng wrote: shuisheng wrote: Dear All; Would you please help me to look at the following case: //! Rotation. enum Rotation { NON_CYCLIC, CYCLIC }; //! Rotation. enum Direction { LEFT, RIGHT }; //! This is a class template of TinyVector template class TinyVector { public: //! Rotate. template TinyVector& Rotate(size_t n); } //! Rotate. template template inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(size_t n) { ... return *this; } //! Rotate. template template<> Reverse those two template declaration. It becomes okay. Quite interesting. template<> template inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(size_t n) { ... return *this; } // <-- Error position Error C2768: 'TinyVector<_T,_n>::Rotate' : illegal use of explicit template arguments I realy appreciate your guys' help. It make me to learn c++ much more comforably. Shuisheng Still WRONG. Help!!! Sep 27 '06 #3

 P: n/a shuisheng wrote: Dear All; Would you please help me to look at the following case: //! Rotation. enum Rotation { NON_CYCLIC, CYCLIC }; //! Rotation. enum Direction { LEFT, RIGHT }; //! This is a class template of TinyVector template class TinyVector { public: //! Rotate. template TinyVector& Rotate(size_t n); } //! Rotate. template template inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(size_t n) { ... return *this; } //! Rotate. template template<> inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(size_t n) { ... return *this; } // <-- Error position You cannot specialize a template member function without first specializing the class template. so you need to first specialize the class template for a specific type. > Error C2768: 'TinyVector<_T,_n>::Rotate' : illegal use of explicit template arguments I realy appreciate your guys' help. It make me to learn c++ much more comforably. Shuisheng Sep 27 '06 #4

 P: n/a shuisheng wrote: //! Rotation. enum Rotation * * * * * { NON_CYCLIC, CYCLIC }; //! Rotation. enum Direction * * * * *{ LEFT, RIGHT }; //! This is a class template of TinyVector template class TinyVector { public: //! Rotate. template TinyVector& Rotate(size_t n); } missing ";": }; that one already causes a lot of confusing error messages to go away. > //! Rotate. template template inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(size_t n) ditch the "typename" { ... return *this; } //! Rotate. template template<> inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(size_t n) { ... return *this; } * // <-- Error position Finally, this last one cannot fly at all: as of the current standard, partial specializations of function templates do not exist in C++. You have to turn that into a class template. The class could be stateless and just provide a static function. You could try something like: #include typedef int Direction; typedef int Rotation; //! This is a class template of TinyVector template class TinyVector; template < Direction dir, Rotation rot > struct alg; template<> struct alg< 0, 0 { template static TinyVector class TinyVector { public: template TinyVector& Rotate(size_t n) { return ( alg::rotate( *this ) ); } }; Best Kai-Uwe Bux Sep 27 '06 #5

 P: n/a shuisheng wrote: template Identifiers which begin with an underscore are reserved for the implementation. Use T and n instead. If you really want to use an underscore, you can use T_ and n_ . Nate Sep 28 '06 #6

 P: n/a Nate Barney wrote: shuisheng wrote: >template Identifiers which begin with an underscore are reserved for the implementation. Only in the global scope. Of course those that begin with a '_' and a capital letter (like "_T" here) are always reserved, no matter what scope. Use T and n instead. If you really want to use an underscore, you can use T_ and n_ . I think many who use the leading underscore think that with it their code looks cool because it looks more like the "internal stuff" or the "implementation detail", or some such. Or, maybe they think it would help avoid naming conflict somewhere down the road (which it actually doesn't). V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask Sep 28 '06 #7

 P: n/a Victor Bazarov wrote: Nate Barney wrote: >Identifiers which begin with an underscore are reserved for theimplementation. Only in the global scope. Of course those that begin with a '_' and a capital letter (like "_T" here) are always reserved, no matter what scope. Interesting, I didn't know that. In any case, it's probably best to eschew leading underscores entirely. Nate Sep 28 '06 #8

### This discussion thread is closed

Replies have been disabled for this discussion.