473,385 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Template parameter matching and const

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
0 912

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Gianni Mariani | last post by:
Does anyone know if this is supposed to work ? template <unsigned N> int strn( const char str ) { return N; } #include <iostream>
7
by: Vaca Louca | last post by:
Hello, My setup: Debian sarge on dual Pentium 4. g++ 3.3.5-3. (the other system is Windows XP with MS Visual Studio .NET 2003) I have an auto_array<T> template (based on a template taken from...
3
by: Capstar | last post by:
Hi NG, I am trying to get the attached piece of code to work, but I can't figure out what I'm doing wrong. To me it seems that when I don't pass an argument to x::do_something, it should use the...
3
by: jois.de.vivre | last post by:
Hi, I have the following code: #include <string> using namespace std; template <class T> class MyTemplate { public: MyTemplate(){ size = 0;
12
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include...
2
by: cablepuff | last post by:
template <typename ContainerType> ContainerType rsa_encrypt_list(const std::string&, const typename ContainerType::reference, const typename ContainerType::reference); const BigInteger...
6
by: abir | last post by:
i have a template template<typename Sclass Indexer{}; i want to have a specialization for std::vector both const & non const version. template<typename T,typename Aclass Indexer<std::vector<T,A...
6
by: abir | last post by:
i have a template as shown template<typename Sclass Indexer{}; i want to have a specialization for std::vector both const & non const version. template<typename T,typename Aclass...
1
by: Ali | last post by:
The code at the end of this message works just fine with M$ VS2005 but with g++ 4.1.3 i get this: node.cpp: In function ‘int main()’: node.cpp:96: error: no matching function for call to...
12
by: nooneinparticular314159 | last post by:
Hello. If I declare the following: template<int a, int b, int SomeArray> class DoSomething{ public: .. .. ..
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.