Re: template copy constructor

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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


.



Relevant Pages

  • Cannot convert from Type to const Type& error with templates
    ... what other people say before I blame the compiler. ... template<typename F, typename G> ... // Template function to construct compose_t ... // Helper function to form arbitrary predicate ...
    (microsoft.public.vc.language)
  • Re: OOP and OOD for senior C-style embedded software engineers
    ... metaprogramming in C++ to whole different level using the template ... templates let the compiler do more work ... The worst thing is when a function throws an exception ... And of course, exception specifications should work like people think they work, not as they /do/ work. ...
    (comp.arch.embedded)
  • Re: OOP and OOD for senior C-style embedded software engineers
    ... metaprogramming in C++ to whole different level using the template ... In particular, templates let the compiler do more work at compile time, and save the target from work at run time. ... Templates can be great for generic libraries, and do not necessarily create code bloat. ... You can use RAII even without exceptions, but obviously you have to handle your errors explicitly. ...
    (comp.arch.embedded)
  • Re: [C++] Vector - guaranteed to be contiguous?
    ... >> member is templated on the input iterators as well. ... template<typename OUTITER, typename INITER, typename INITERT> ... What happens is that the compiler sees copy and thinks, "Hey, I know copy. ... But it is a template, let me try to deduce those types. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: generic programming is very powerful. c should support it
    ... Where the compiler matches the correct template. ... already happens differently on different implementations. ...
    (comp.lang.c)