deducting class template parameters from its ctor arguments

H

Hicham Mouline

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,
 
G

gob00st

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
 
H

Hicham Mouline

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
 
H

Hicham Mouline

Hicham Mouline said:
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,
 
J

James Kanze

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top