deducting class template parameters from its ctor arguments

Discussion in 'C++' started by Hicham Mouline, Feb 19, 2009.

  1. hello,

    template <typename A>
    class X {
    public:
    X( const A& );
    private:
    const A& a_;
    };


    user code: Is there a way to have arg A deducted in this code:
    double d =5.6;
    X x(d);


    I would have used a templated ctor but, i need to store the private member
    variable as well.

    rds,
    Hicham Mouline, Feb 19, 2009
    #1
    1. Advertising

  2. Hicham Mouline

    Guest

    On Feb 19, 12:26 pm, "Hicham Mouline" <> wrote:
    > hello,
    >
    > template <typename A>
    > class X {
    > public:
    >   X( const A& );
    > private:
    >   const A& a_;
    >
    > };
    >
    > user code: Is there a way to have arg A deducted in this code:
    >  double d =5.6;
    >  X x(d);
    >
    > I would have used a templated ctor but, i need to store the private member
    > variable as well.
    >
    > rds,


    Check FAQ first, there answer is there.
    But FWIW,
    <quote>
    unlike template functions, template classes (instantiations of class
    templates) need to be explicit about the parameters over which they
    are instantiating.
    </quote>

    Regards
    Gob00st
    , Feb 19, 2009
    #2
    1. Advertising

  3. <> wrote in message
    news:...
    On Feb 19, 12:26 pm, "Hicham Mouline" <> wrote:
    > hello,
    >
    > template <typename A>
    > class X {
    > public:
    > X( const A& );
    > private:
    > const A& a_;
    >
    > };
    >
    > user code: Is there a way to have arg A deducted in this code:
    > double d =5.6;
    > X x(d);
    >
    > I would have used a templated ctor but, i need to store the private member
    > variable as well.
    >
    > rds,


    Check FAQ first, there answer is there.
    But FWIW,
    <quote>
    unlike template functions, template classes (instantiations of class
    templates) need to be explicit about the parameters over which they
    are instantiating.
    </quote>

    So there is no way to have a_ a template private member and at the same time
    be able to construct objects of type of a full spec of X without explicitly
    specifying A...

    ok, thanks
    Hicham Mouline, Feb 19, 2009
    #3
  4. "Hicham Mouline" <> wrote in message
    news:499d7040$0$90263$...
    > <> wrote in message
    > news:...
    > On Feb 19, 12:26 pm, "Hicham Mouline" <> wrote:
    >> hello,
    >>
    >> template <typename A>
    >> class X {
    >> public:
    >> X( const A& );
    >> private:
    >> const A& a_;
    >>
    >> };
    >>
    >> user code: Is there a way to have arg A deducted in this code:
    >> double d =5.6;
    >> X x(d);
    >>
    >> I would have used a templated ctor but, i need to store the private
    >> member
    >> variable as well.
    >>
    >> rds,

    >
    > Check FAQ first, there answer is there.
    > But FWIW,
    > <quote>
    > unlike template functions, template classes (instantiations of class
    > templates) need to be explicit about the parameters over which they
    > are instantiating.
    > </quote>
    >
    > So there is no way to have a_ a template private member and at the same
    > time
    > be able to construct objects of type of a full spec of X without
    > explicitly specifying A...
    >
    > ok, thanks


    I hadn't defined the problem completely.
    The annoyance of having to specify the template arguments comes from the
    fact that X
    takes 3 template arguments:

    template <typename A, typename B, typename C>
    class X {
    X(...)
    };

    So I was looking for a convenient way to deduct the template arguments when
    constructing the object of type X
    It dawned on me : std::make_pair() is the example of convenience I was
    looking for... and extending that to boost::tuple,
    make_tuple is what I want.

    Except I do not necessarily wish the user to work directly with tuples for
    my type X.

    How can I duplicate the desired effect for X?

    Should i implemement a temlate function make_X exactly the way make_tuple
    and make_pair are implemented?

    rds,
    Hicham Mouline, Feb 19, 2009
    #4
  5. Hicham Mouline

    James Kanze Guest

    On Feb 19, 1:26 pm, "Hicham Mouline" <> wrote:

    > template <typename A>
    > class X {
    > public:
    > X( const A& );
    > private:
    > const A& a_;
    > };


    > user code: Is there a way to have arg A deducted in this code:
    > double d =5.6;
    > X x(d);


    > I would have used a templated ctor but, i need to store the
    > private member variable as well.


    The short answer is no. Depending on actual use patterns, you
    can sometimes get the same effect by creating a function which
    returns an instance, e.g.:

    template< typename A >
    X< A >
    getX( A const& a )
    {
    return X< A >( a ) ;
    }

    This works, for example, if the instance is going to be used as
    a temporary. It doesn't give you the ability to have a named
    value of the type, however; for that, you'll need C++0x.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Feb 19, 2009
    #5
    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. Apricot
    Replies:
    4
    Views:
    511
    velthuijsen
    Apr 16, 2004
  2. NVH
    Replies:
    8
    Views:
    477
    mlimber
    Jul 6, 2006
  3. Grizlyk
    Replies:
    8
    Views:
    463
    Grizlyk
    Nov 29, 2006
  4. , India

    copy ctor vs default ctor

    , India, Aug 15, 2007, in forum: C++
    Replies:
    2
    Views:
    398
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 15, 2007
  5. Bill

    Deducting values in SQL

    Bill, Jul 28, 2003, in forum: ASP General
    Replies:
    0
    Views:
    97
Loading...

Share This Page