Archived

This topic is now archived and is closed to further replies.

template to avoid name collisions

This topic is 5397 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

//data

public:
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 }

};

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites