From: Vitaly on
Hi,

I have a need to provide an iterator-like interface to a collection of
unrelated types.
The first part of the problem is building this sort of collection --
Boost.Any seems to be a good option, as I can create a
std::vector<boost::any> and add int, float or any other type of
objects to it.

The part that I haven't been able to solve, is how to go about
creating a dereference function that will return the correct type of
object. Overloading won't work, as only the return type will change.

The types have a similar interface e.g.

template <class T>
class List
{
public:
T f() {}
private:
T *data;
};

I don't think a common base class approach will work, since only the
subclasses will know the type T.

Does anyone have any ideas?


Thanks,

Vitaly

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Larry Evans on
On Jul 23, 7:04 am, Vitaly <vbudov...(a)gmail.com> wrote:
> Hi,
>
> I have a need to provide an iterator-like interface to a collection of
> unrelated types.

What about a boost::fusion sequence?

http://www.boost.org/doc/libs/1_43_0/libs/fusion/doc/html/fusion/sequence.ht
ml

Each comes with a iterator:

http://www.boost.org/doc/libs/1_43_0/libs/fusion/doc/html/fusion/iterator.ht
ml


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel Krügler on
On 23 Jul., 14:04, Vitaly <vbudov...(a)gmail.com> wrote:
> Hi,
>
> I have a need to provide an iterator-like interface to a collection of
> unrelated types.
> The first part of the problem is building this sort of collection --
> Boost.Any seems to be a good option, as I can create a
> std::vector<boost::any> and add int, float or any other type of
> objects to it.
>
> The part that I haven't been able to solve, is how to go about
> creating a dereference function that will return the correct type of
> object. Overloading won't work, as only the return type will change.
>
> The types have a similar interface e.g.
>
> template <class T>
> class List
> {
> public:
> T f() {}
> private:
> T *data;
>
> };
>
> I don't think a common base class approach will work, since only the
> subclasses will know the type T.
>
> Does anyone have any ideas?

If you are asking: What is the proper reference type for
my iterator, this can only be boost::any or any other type
that wraps a boost::any or does behave as a boost::any.

Obviously this does not give you the capability to provide
a compile-time mechanism that returns the underlying
type. In fact, this is impossible (as you already assume),
because the underlying type is part of the dynamic nature
of boost::any or similar types.

boost::any gives you a member type() that allows one to
determine the underlying type (std::type_info), though.

Now you have to decide on your own, how you can take
advantage of this feature:

If the possible set of underlying types is known in
advance, you can provide a function that looks at the
type() and switches on possible known types (hard to
manage), but rather quick (possibly also dirty).

Instead of this hard-coded set of known types you
provide an API, that allows registering handler functions
/function objects based on the std::type_info (or
in C++0x: std::typeindex) as the key and the (smart)
pointer to the handler as a value, e.g. given

typedef void(*handler_t)(boost::any&);
std::[unordered_]map<std::typeindex, handler_t> known_handlers;
std::vector<boost::any> my_data;

this allows for the following traversal (I use here the new C++0x
for-range syntax):

for (boost::any& datum : my_data) {
known_handlers.at(datum.type())(datum);
}

HTH & Greetings from Bremen,

Daniel Kr�gler


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On Jul 23, 1:04 pm, Vitaly <vbudov...(a)gmail.com> wrote:
> Hi,
>
> I have a need to provide an iterator-like interface to a collection of
> unrelated types.
> The first part of the problem is building this sort of collection --
> Boost.Any seems to be a good option, as I can create a
> std::vector<boost::any> and add int, float or any other type of
> objects to it.
>
> The part that I haven't been able to solve, is how to go about
> creating a dereference function that will return the correct type of
> object.

So basically, you want a different static type depending on a runtime
criterion.
You realize this cannot work right?


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Vitaly on
On Jul 23, 10:04 pm, Vitaly <vbudov...(a)gmail.com> wrote:
> Hi,
>
> I have a need to provide an iterator-like interface to a collection of
> unrelated types.
> The first part of the problem is building this sort of collection --
> Boost.Any seems to be a good option, as I can create a
> std::vector<boost::any> and add int, float or any other type of
> objects to it.
>
> The part that I haven't been able to solve, is how to go about
> creating a dereference function that will return the correct type of
> object. Overloading won't work, as only the return type will change.
>
> The types have a similar interface e.g.
>
> template <class T>
> class List
> {
> public:
> T f() {}
> private:
> T *data;
>
> };
>
> I don't think a common base class approach will work, since only the
> subclasses will know the type T.
>
> Does anyone have any ideas?
>
> Thanks,
>
> Vitaly
>


I feel I need to expand on what I said earlier. Here is what I'm
trying to achieve:

I have a data file that contains several arrays of data. I read in
each of these arrays and construct the appropriate List<T> object for
each of them.
List<T> has an iterator interface. What I would like to now do is
store these list objects in some sort of container. I would also like,
if possible, to provide an iterator interface to be able to iterate
through these List objects. Another option that I would consider, is
to provide a find(key) function, that will return a List<T> object
from this container. Hopefully my intent is clear now. Thank you for
the replies so far.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]