Re: template copy constructor
- From: "Vladimir Grigoriev" <vlad.moscow@xxxxxxx>
- Date: Wed, 7 Oct 2009 15:35:55 +0400
I am sorry. I did not mention that I use Visual C++ 2005 EE.
This compiler does not consider
template <typename U>
A( A<U> &rhs ){}
as a template copy constructor. It considers the function as an usual
constructor with one parameter.
I tried the same operation with std:auto_ptr and I got the same compilation
error
The statement
std::auto_ptr<Y> py2( new Y );
std::auto_ptr<X> px2 = py2;
------ Build started: Project: auto_ptr_design1, Configuration: Debug
Win32 ------
Compiling...
auto_ptr_design1.cpp
c:\documents and settings\vgrigoryev\my documents\visual studio
2005\projects\auto_ptr_design1\auto_ptr_design1.cpp(214) : error C2440:
'initializing' : cannot convert from 'std::auto_ptr<_Ty>' to
'std::auto_ptr<_Ty>'
with
[
_Ty=Y
]
and
[
_Ty=X
]
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called
Build log was saved at "file://c:\Documents and Settings\vgrigoryev\My
Documents\Visual Studio 2005\Projects\auto_ptr_design1\Debug\BuildLog.htm"
auto_ptr_design1 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Vladimir Grigoriev
"David Wilkinson" <no-reply@xxxxxxxxxxxx> wrote in message
news:%23ZrvfD0RKHA.3876@xxxxxxxxxxxxxxxxxxxxxxx
Vladimir Grigoriev wrote:
Let consider the code
template <typename T>
class A
{
public:
A( T value = 0 ){}
A( A<T> &rhs ){} // without const attribute
template <typename U>
A( A<U> &rhs ){} // without const attribute
};
main()
{
A<int> a1;
A<int> a2 = a1; // this is O'k
A<double> a3( a1 ); // this is O'k because A( A<U> &rhs ) is
called
A<double> a4 = a1; // here is a compilation array
}
What should I add (maybe some operato function) rthat the last statement
will be compiled? I.e. what is the appropriate sequence of type
conversions that is needed to compile the code taking into account that
the template constructor with const reference should not be used?
Vladimir:
Code compiles for me on VS2008 (if I correct to int main()).
--
David Wilkinson
Visual C++ MVP
.
- Follow-Ups:
- Re: template copy constructor
- From: Igor Tandetnik
- Re: template copy constructor
- From: David Wilkinson
- Re: template copy constructor
- From: Vladimir Grigoriev
- Re: template copy constructor
- References:
- template copy constructor
- From: Vladimir Grigoriev
- Re: template copy constructor
- From: David Wilkinson
- template copy constructor
- Prev by Date: Re: template copy constructor
- Next by Date: Re: IPC
- Previous by thread: Re: template copy constructor
- Next by thread: Re: template copy constructor
- Index(es):
Relevant Pages
|