Prev: Does ternary conditional operator bind the order of evaluation?
Next: Is there any standard/guarantees for exception safety in STL operations?
From: tni on 25 Jul 2010 00:18 If your set of types is known/fixed at compile time, Boost Variant is what you want: http://www.boost.org/doc/libs/1_43_0/doc/html/variant.html In general, you will want to use visitation (which Boost Any doesn't provide). This works quite well with template visitation code / duck typing. If you throw in Boost MPL (to manage your type list) and enable_if, you can do all sorts of neat things (including iteration over the type list). -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Vitaly on 25 Jul 2010 23:11 On Jul 26, 1:18 am, tni <t...(a)example.invalid> wrote: > If your set of types is known/fixed at compile time, Boost Variant is > what you want: > > http://www.boost.org/doc/libs/1_43_0/doc/html/variant.html > > In general, you will want to use visitation (which Boost Any doesn't > provide). This works quite well with template visitation code / duck > typing. If you throw in Boost MPL (to manage your type list) and > enable_if, you can do all sorts of neat things (including iteration > over the type list). > I don't have any experience with the MPL library. Could you please provide an example of how I could achieve this (iteration)? A variant that just accepts int/float would be fine. I've been reading up on MPL/ Fusion but it's a lot to take in. Thanks. -- [ 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 26 Jul 2010 02:46 On 25 Jul., 16:26, Vitaly <vbudov...(a)gmail.com> wrote: > On Jul 23, 10:04 pm, Vitaly <vbudov...(a)gmail.com> wrote: [..] > 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. I still don't understand how you would like to interact with the container - a code snippet would probably help. Provocative question: Why does boost::tuple<List<A>, List<B>, ..., List<Z>> not work? If it important that the container itself does not reflect the types of each list element by it's own type (this is what you where basically addressing by declaring List<List<boost::any>>), the "key" must be a type or constant number, it cannot be itself a dynamic key. E.g. the get<> function template to retrieve the i-the element of a tuple works, because the template argument is a constant expression of type std::size_t. Or for another example: It is possible to query std::function for the actually hold target type, but the query has to suggest the type, it cannot be a key of a type which is not known during compile-time. 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: Vitaly on 26 Jul 2010 23:42
> I still don't understand how you would like to interact > with the container - a code snippet would probably > help. > > Provocative question: Why does > > boost::tuple<List<A>, List<B>, ..., List<Z>> > > not work? If it important that the container itself > does not reflect the types of each list element > by it's own type (this is what you where basically > addressing by declaring List<List<boost::any>>), > the "key" must be a type or constant number, it > cannot be itself a dynamic key. E.g. the get<> > function template to retrieve the i-the element of > a tuple works, because the template argument > is a constant expression of type std::size_t. > Or for another example: It is possible to query > std::function for the actually hold target type, > but the query has to suggest the type, it cannot > be a key of a type which is not known during > compile-time. > > HTH & Greetings from Bremen, > > Daniel Kr�gler Here is some code: // Externally defined class. This just contains an array // of data and information describing it. The array is extracted // into the List class. class Source { }; template<class T> class List { template<class Value> class raster_band_iterator: public boost::iterator_facade< raster_band_iterator<Value>, Value, boost::forward_traversal_tag> { // Left out for brevity. }; public: typedef ListIterator<T> iterator; typedef ListIterator<const T> const_iterator; // Initialises the data_ variable with the array from the external //source. List(Source *src); iterator begin(); iterator end(); private: T *data_; }; class ListContainer { public: ListContainer(std::string filename) { /* Some pseudocode ... Open filename. for_each(Source, in file) { if(Source.type == int) { lists.push_back(List<int>(source)); } else if(Source.type == float) { lists.push_back(List<float>(source)); } ... and a few others ... } */ } // Ideally I would like to be able to iterate over the List<T> // objects. iterator begin(); iterator end(); // Would be good if this sort of function was available also. // This of course assumes that the List<T> objects would be // stored in some sort of map container. /* SomeReturnType */ find(std::string key); private: // I'm open to ideas on how to best store the List<T> objects. std::vector<boost::any> lists_; }; int main() { ListContainer l1("file.1"); // This is just an example usage. It doesn't have to look exactly // like that in the end. But it would be good to iterate over // List<T> objects and also over their contents. for(/*SomeIterType */ i = l1.begin(); i != l1.end(); ++i) { for(/* SomeIterType2 */ j = i->begin(); j != i->end(); ++j) { // Do stuff. } } /*SomeIterType */ obj = l1.find("some list"); for(/* SomeIterType2 */ j = obj->begin(); j != obj->end(); ++j) { // Do stuff. } } I hope it's now clear what I'm after. Thanks. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |