By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 914 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Template parameter matching and const

P: n/a
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

Jul 22 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.