Sign in to follow this  

Template specialization and hidden handles

This topic is 3490 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

I am trying to find a convenient way to handle element handles without explicitly needing to use them, the problem at the moment is that it's not possible to specialize the templates in the same class scope. For instance, we have two classes, and one container class of arbitrary elements with a handle pointer requirement:

class Object{
public:
  Handle handle;
};

template <class T, Handle T::*handleptr>
class Con{
public:
  ...
};

class User{
public:
  Con<Object,&Object::handle> objects; //Valid
  Con<User,&User::handle> subusers; //invalid because of class scope
  Handle handle;
};


What I am looking for is a similar way to handle elements as the first Container in user, not needing to "care" about their respective handles when dealing with classes able to contain lists of the same type as themselves. One option is ofc. to inherit and use it as an extension, however it fails if several handles are needed. Also another option of declaring a subclass with the handle is quite crude. Any input is welcome, thank you.

Share this post


Link to post
Share on other sites
So far the most elegant solution I have found is:



template <class T, Handle T::*handleptr, class Convert = T>
class Con{
public:
Convert * Retrieve(int i){
return (Convert*) buffer[i];
}
...
};

class _User{
public:
Handle handle;
};

class User : public _User{
public:
Con<_User,&_User::handle,User> subusers; //valid, however still quite crude
};




Passing the type with which to use the container externally. An acceptable solution, however far from elegant.
Any suggestions on improvements?

Share this post


Link to post
Share on other sites
I'm not exactly sure what you want to do, but doesn't this work for you?

class Object {
public:
Handle handle;
};

template < typename T >
class Con {
Handle T::*hptr;
public:
Con() : hptr( &T::handle ) { }
};

class User {
public:
Con< Object > objects;
Con< User > subusers;
Handle handle;
};

Share this post


Link to post
Share on other sites
Thanks for the reply.

Well, since it assumes every class to have the "handle" with that specific name, and this is not the case.
It is quite exactly the same solution as to inherit the handle and using the type directly in the container as the handle.

Any class may have a handle or several with any names, it would work if it was divided into two distinct container classes, one assumes the one handle, and one that expects the template parameter. A possible solution, however leaves alot to be desired since it does not allow for the multiple or arbitrary named handle cases.

Passing a variable name would be nice as a template parameter, but thats just dreaming of the impossible ^^

Any further input is greatly appreciated.

Share this post


Link to post
Share on other sites

This topic is 3490 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this