• [^] # Re: Constructeur de copie

    Posté par . En réponse au message Petit soucis avec les shared_ptr. Évalué à 2.

    Ben avec gcc et clang ça ne marche pas chez moi.

    In file included from main.cpp:1:
    In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/iostream:39:
    In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/ostream:38:
    In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/ios:42:
    In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/ios_base.h:41:
    In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/locale_classes.h:40:
    In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/string:41:
    In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/allocator.h:46:
    In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/x86_64-pc-linux-gnu/bits/c++allocator.h:33:
    /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/ext/new_allocator.h:120:23: error: no matching constructor for initialization of 'MyInt'
     { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
     ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/alloc_traits.h:253:8: note: in instantiation of function template specialization '__gnu_cxx::new_allocator<MyInt>::construct<MyInt, MyInt *>' requested here
     { __a.construct(__p, std::forward<_Args>(__args)...); }
     ^
    /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/alloc_traits.h:399:4: note: in instantiation of function template specialization 'std::allocator_traits<std::allocator<MyInt> >::_S_construct<MyInt, MyInt *>' requested here
     { _S_construct(__a, __p, std::forward<_Args>(__args)...); }
     ^
    /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/shared_ptr_base.h:515:30: note: in instantiation of function template specialization 'std::allocator_traits<std::allocator<MyInt> >::construct<MyInt, MyInt *>' requested here
     allocator_traits<_Alloc>::construct(__a, _M_ptr(),
     ^
    /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/ext/new_allocator.h:120:23: note: in instantiation of function template specialization 'std::_Sp_counted_ptr_inplace<MyInt, std::allocator<MyInt>, 2>::_Sp_counted_ptr_inplace<MyInt *>' requested here
     { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
     ^
    /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/alloc_traits.h:253:8: note: in instantiation of function template specialization '__gnu_cxx::new_allocator<std::_Sp_counted_ptr_inplace<MyInt, std::allocator<MyInt>, 2>
     >::construct<std::_Sp_counted_ptr_inplace<MyInt, std::allocator<MyInt>, 2>, const std::allocator<MyInt>, MyInt *>' requested here
     { __a.construct(__p, std::forward<_Args>(__args)...); }
     ^
    /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/alloc_traits.h:399:4: note: (skipping 2 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all)
     { _S_construct(__a, __p, std::forward<_Args>(__args)...); }
     ^
    /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/shared_ptr_base.h:1089:14: note: in instantiation of function template specialization 'std::__shared_count<2>::__shared_count<MyInt, std::allocator<MyInt>, MyInt *>' requested here
     : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
     ^
    /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/shared_ptr.h:316:4: note: in instantiation of function template specialization 'std::__shared_ptr<MyInt, 2>::__shared_ptr<std::allocator<MyInt>, MyInt *>' requested here
     : __shared_ptr<_Tp>(__tag, __a, std::forward<_Args>(__args)...)
     ^
    /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/shared_ptr.h:587:14: note: in instantiation of function template specialization 'std::shared_ptr<MyInt>::shared_ptr<std::allocator<MyInt>, MyInt *>' requested here
     return shared_ptr<_Tp>(_Sp_make_shared_tag(), __a,
     ^
    /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/shared_ptr.h:603:19: note: in instantiation of function template specialization 'std::allocate_shared<MyInt, std::allocator<MyInt>, MyInt *>' requested here
     return std::allocate_shared<_Tp>(std::allocator<_Tp_nc>(),
     ^
    main.cpp:91:13: note: in instantiation of function template specialization 'std::make_shared<MyInt, MyInt *>' requested here
     auto p1 = make_shared<MyInt>(&MyInt::get(3));
     ^
    main.cpp:13:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'MyInt *' to 'const MyInt' for 1st argument; dereference the argument with *
    class MyInt {
     ^
    main.cpp:13:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'MyInt *' to 'MyInt' for 1st argument; dereference the argument with *
    class MyInt {
     ^
    main.cpp:27:3: note: candidate constructor not viable: no known conversion from 'MyInt *' to 'int' for 1st argument
     MyInt(int n):id(n){}
     ^
    1 error generated.