Choose template return type?

Discussion in 'C++' started by Joseph Turian, Jan 20, 2007.

  1. Examples is a vector of some EXAMPLE type (templated).
    ExamplePtrs is a vector of some EXAMPLE* type.
    They both contain the same methods, except the latter is dereferencing
    pointers whereas the former is just working with EXAMPLE objects
    directly.
    (I'm sure there's a smarter way to do this with template without
    defining two classes, but whatever. It works for me now, and my
    question is about something else.)

    I want to write a method Examples::ptrs() to return a boost::shared_ptr
    to an ExamplePtrs type:

    ===

    template<typename EXAMPLE> class ExamplePtrs;

    template <typename EXAMPLE>
    class Examples : public vector<EXAMPLE> {
    public:
    ...
    template<typename EXAMPLE2> boost::shared_ptr<ExamplePtrs<EXAMPLE2>
    > ptrs() const;

    };


    template <typename EXAMPLE>
    class ExamplePtrs : public vector<EXAMPLE*> {
    public:
    ....
    }

    ===

    Notice that Examples::ptrs() is templated, because I would like---for
    example---to take a Examples<E> and call ptrs() and have it return a
    boost::shared_ptr<ExamplePtrs<const E> > (const'ing the ptrs). So far,
    the above syntax seems okay. But the question is, how do I call
    function ptrs()?

    Here's what I'm trying to do, except it doesn't compile:
    Examples<E> exmpls;
    boost::shared_ptr<ExamplePtrs<const E> > eptrs = exmpls.ptrs<const
    E>();

    How can I specify the specific type instantiation needed?

    Thanks,

    Joseph
     
    Joseph Turian, Jan 20, 2007
    #1
    1. Advertisements

  2. Joseph Turian wrote:
    > [..]
    > Here's what I'm trying to do, except it doesn't compile:
    > Examples<E> exmpls;
    > boost::shared_ptr<ExamplePtrs<const E> > eptrs = exmpls.ptrs<const
    >> ();


    I think it should be

    ... eptrs = exmpls.template ptrs<const E>();

    > How can I specify the specific type instantiation needed?


    It's not the problem in specifying the type, I believe.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 20, 2007
    #2
    1. Advertisements

  3. > I think it should be
    > .. eptrs = exmpls.template ptrs<const E>();


    That worked great, thanks!

    Joseph
     
    Joseph Turian, Jan 20, 2007
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert A. T. Kaldy

    template typedef as return type

    Robert A. T. Kaldy, Jul 9, 2003, in forum: C++
    Replies:
    1
    Views:
    524
    Michael Kochetkov
    Jul 9, 2003
  2. Damon
    Replies:
    2
    Views:
    11,562
    Damon
    Dec 15, 2003
  3. Replies:
    3
    Views:
    561
    Victor Bazarov
    Nov 19, 2004
  4. Pete Becker
    Replies:
    5
    Views:
    1,160
    =?iso-8859-1?Q?Ali_=C7ehreli?=
    Nov 19, 2004
  5. Greenhorn
    Replies:
    15
    Views:
    1,105
    Keith Thompson
    Mar 6, 2005
  6. andrew cooke
    Replies:
    1
    Views:
    276
    Steven D'Aprano
    Apr 3, 2011
  7. Replies:
    2
    Views:
    276
  8. 邹俊洋
    Replies:
    0
    Views:
    257
    邹俊洋
    Aug 12, 2013
Loading...