I am trying to write a little library which allows the user to implement certain behaviours so that the library may work with his own underlying data structure. In this particular case (although I'm simplifying), I'm trying to define something like this:
class LibraryClass {
virtual std::vector<int>::const_iterator intsBegin() const = 0;
virtual std::vector<int>::const_iterator intsEnd() const = 0;
}
In other words, the library will accept to work with any derived class which allows it to iterate over a vector of integers. Now the tricky bit is: I don't want to force the use of vectors. Really, any iterator over integers is okay for me, as long as I can detect when it ends, and dereference it to an integer. Here's the missing bit:
class LibraryClass {
virtual /* ??? */ intsBegin() const = 0;
virtual /* ??? */ intsEnd() const = 0;
}
class UserClass {
/* This should be allowed, here I'm using vector as an example. */
std::vector<int>::const_iterator intsBegin() const;
std::vector<int>::const_iterator intsEnd() const;
}
Now, I know how to have that kind of behaviour through templates for method parameters, but I can't find a way to have it for a return value in a virtual method, given that:
- Whether the iterator is
constor not isn't very important. - There's won't be any default behaviour in the base class.
- The mechanism mustn't be bugged by inheritance: classes derived from
intiterators should be accepted as a return type as well, since they meet the requirements. I don't think this is an issue, because transitivity, but still. - Ideally, the
beginandendmethods should be forced to have the same iterator type in a given derived class. - Even more ideally, I'd like to avoid using Boost for things like
enable_if. The library is written using C++11 (at least I try).
Aucun commentaire:
Enregistrer un commentaire