template to avoid name collisions

Started by
6 comments, last by svpace 21 years, 1 month ago
Hi, this is a idea I was developing that could use some help. I have a class A that have around 200 fields each of then have a very similar consistency check. reimplementing the same check in the 200 fields seens very inproductive, so here is my idea create a class templatized class Field, and use partial specialization to be able to descend the class A from several Field classes. The below code don''t compile but will give a more clear view of the situation: template class Field { private: // data public: template void set(const T& x) { /* throw exception */ } template<> set void set(const T& x) { /* do check */ } }; const int FIELD000 0; const int FIELD001 1; ... const int FIELD199 199; class A : public Field, public Field, ... public Field { /* rest of the class */ }; So, I could use something like that A a; a.set(1); a.set(2.0f); The compiler complains that I can''t use partial specialization within the class declaration, so I tried something like this outside the class: template template<> void Field::set(const T& x) {/* do check */}; still unsucessfull, any ideas?
Advertisement
Even "already tried, it''s impossible" will be welcome
HUNGARIAN NOTATION!!!!

microsoft invented it...
Programmers of the world, UNTIE!
You need to look again at how you declare templates.
It should look something like this:

  template<typename T>class Field{private: // make this protected if you want the derived classes to access this, but only if the data is based upon type T, because of ambiguity of the names//datapublic:    void set(const T& x) { //default implementation } // notice there is no template needed here};  

You then implement the method in the derived class e.g.

  class A: public Field<int>{public:      void set(const int & x) { // implement code }};  
MatS423: Need more information, don''t have a clue about what are you talking.

jamessharp: I think you missed the point here, I don''t want to overload the set method, all set''s will have the same generic implementation, and A will be a subclass of 200 specializations of Field, the idea is to avoid name collision using template specialization, see:

  template<unsigned int N>class F { public: set() {...} };class A : public F<0>, public F<1>, public F<2> {} a;a.set() // ambiguity, can be F<0>::set(), F<1>::set(), etc  


What I want is to use the specialization as a form to resolve the ambiguity.
Function templates don''t support partial template specialization (well, they do support full specialization), but how about this:

  template <class T> class Field {private:   // data public:  void set(const T& x) { /* do check */ }};class A {public:  Field<int> field000;  Field<float> field001;  ...  Field<double> field199;/* rest of the class */};A a;a.field000.set(1);a.field001.set(2.0f);  
.

[edited by - civguy on March 4, 2003 6:54:37 PM]
So, svpace, is that what you wanted? I dislike it when I post an answer I think is right and the original poster doesn''t even bother to check it. Is there a reason you wanted to use inheritance instead of member variables?

This topic is closed to new replies.

Advertisement