Re: trait technique?

From: Nate Barney (natebarney_at_nospam.adelphia.net)
Date: 12/19/04


Date: Sun, 19 Dec 2004 14:48:26 -0500

Jonathan Mcdougall wrote:
> Patrick Guio wrote:
>
>> Oops, my fault, it actually works... But I have another question: is
>> there a way to have a container such as map or vector that would
>> contain pointers of different specialisations of the template (a kind
>> of polymorphic pointer for template class)?
>
>
> A container cannot contain different types. A vector of int will always
> store ints. Since a class template is not a type (it is a family of
> types), you cannot use it as an element type for a container.
>
> template <class T> struct S;
>
> std::vector< S > v; // illegal
> std::vector< S* > v; // illegal
>
> std::vector< S<int> > v; // ok
> std::vector< S<int>* > v; // ok

This may or not be useful to the OP, but you can do this:

template <class T> struct S;

template <class T2>
struct S2
{
        std::vector<S<T2> > v;
        std::vector<S<T2>*> v2;
};



Relevant Pages

  • Re: pointers to elements in containers
    ... The 'key_fields' container is meant to store pointers to the ... so Detail's constructor looks like the ... pointers to elements in 'fields' point to valid ... Is your Detail struct being copied? ...
    (microsoft.public.vc.language)
  • Re: order of a node
    ... 28 struct pComplex *sysStats; ... There will be some other container for the pointers. ... enough to establish a list or tree or other useful node structure. ...
    (comp.programming)
  • Re: Statically AND Dynamically Typed Language ??
    ... (say easier to implement the language) ... typing instead of with the template mechanisms? ... element type you have an independent instance of the container type. ... You mean for generics, right? ...
    (comp.lang.misc)
  • Re: Maintance of c++ code
    ... iterator constant in the standard, ... template ... template <class Iter_> ... container, some only work with particular containers. ...
    (comp.object)
  • Re: Teaching 5th graders
    ... used a heavy tissue paper over the cups and I had pre-cut 'windsheilds' of clay. ... I like the idea of the toothbrush and tarpaper - may use it ... Wrap the paper on the container ... Your template will ...
    (rec.crafts.pottery)