Template parameter matching and const

  • Thread starter Rogier van Dalen
  • Start date
R

Rogier van Dalen

Hi all,

I am programming a smart pointer class and want to make the smart_ptr's
constructor to construct the new type.
(see also http://www.gotw.ca/gotw/056.htm, example 4(b))

template <typename T /*,...*/> class smart_ptr {
T * object;
//....
public:
struct construct_object_tag {};
// ....
smart_ptr (construct_object_tag)
: object (new T) {}

template <class A1>
smart_ptr (construct_object_tag, A1 & a1)
: object (new T (a1)) {}

template <class A1, class A2>
smart_ptr (construct_object_tag, A1 & a1, A2 & a2)
: object (new T (a1, a2)) {}
// Etcetera
};

This way, I want to be able to say for example
smart_ptr <int> i (construct_object_tag(), 5);
to get a smart_ptr to an int that is constructed and initialised with 5
at the same time.

Visual C++ 6 instantiates the second constructor with A1 = const int.
gcc however tries to use A1 = int, which obviously doesn't work because
the literal 5 can't be converted to int &. (I think the latter is
standard-compliant.)

Could anyone suggest a way to make this work with gcc (and other
standard-compliant compilers)? Defining
template <class A1, class A2>
smart_ptr (construct_object_tag, const A1 & a1, A2 & a2)
: object (new T (a1, a2)) {}
etcetera for all constructors is not an option, because the number of
constructors needed grows exponentially with the number of constructor
arguments?

Any suggestions are appreciated.

Regards,
Rogier
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top