template constructor in non-template class

Discussion in 'C++' started by Alexander Stippler, Dec 1, 2004.

  1. Hello,

    I want to write a non-template class with a constructor template member.
    The constructor shall not take arguments. Is this possible at all?
    Here the concrete example: A class which contains three vectors. There
    are several different methods to initialize these vectors(size and
    contents). These methods are the template parameter I want to use.

    enum Method {A, B, C, D, E, F};

    class Butcher
    {
    public:
    template <Method m>
    Butcher();
    private:
    Vector a, b, c;
    };

    template <>
    Butcher::Butcher<A>()
    : a(2), b(4), c(2)
    {
    a = 0.5, 1;
    b = -1, 1, 3, 2;
    c = 0, 1;
    }

    ....

    I do not want to call a template function from a normal constructor because
    I would have to allocate the vectors twice. Is there a way. I think the
    trouble is that constructors are unnamed and I have no argument, from which
    the template parameter value can be derived.

    regards,
    alex
     
    Alexander Stippler, Dec 1, 2004
    #1
    1. Advertising

  2. Alexander Stippler

    Tom Widmer Guest

    On Wed, 01 Dec 2004 11:39:47 +0100, Alexander Stippler
    <-ulm.de> wrote:

    >Hello,
    >
    >I want to write a non-template class with a constructor template member.
    >The constructor shall not take arguments. Is this possible at all?


    No. A workaround is to add a dummy argument of the type

    template <Method m>
    class MethodWrapper{};

    or similar to the constructor.

    Tom
     
    Tom Widmer, Dec 1, 2004
    #2
    1. Advertising

  3. Alexander Stippler

    Tom Widmer Guest

    On Wed, 01 Dec 2004 13:21:03 +0000, Tom Widmer
    <> wrote:

    >On Wed, 01 Dec 2004 11:39:47 +0100, Alexander Stippler
    ><-ulm.de> wrote:
    >
    >>Hello,
    >>
    >>I want to write a non-template class with a constructor template member.
    >>The constructor shall not take arguments. Is this possible at all?

    >
    >No. A workaround is to add a dummy argument of the type
    >
    >template <Method m>
    >class MethodWrapper{};
    >
    >or similar to the constructor.


    Or you could create a templated factory function instead of templating
    the constructor.

    Tom
     
    Tom Widmer, Dec 1, 2004
    #3
    1. Advertising

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. mrstephengross
    Replies:
    5
    Views:
    612
    Larry I Smith
    May 18, 2005
  2. ali
    Replies:
    4
    Views:
    590
    David Harmon
    Mar 5, 2007
  3. Rahul
    Replies:
    16
    Views:
    1,224
    terminator
    Nov 7, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,288
  5. nguillot
    Replies:
    5
    Views:
    539
Loading...

Share This Page