Recursive variadic template method operating on tuple - c++

I'm trying to make a method which constructs a tuple from provided parameters, and 'fills' it recursively. Unfortunately, it yields compilation errors... This is the code:
template<typename Head, typename... Tail>
std::vector<IntersectionComponents<Head, Tail...>> intersection() {
std::vector<IntersectionComponents<Head, Tail...>> results;
auto& headComponents = *getAllComponents<Head>();
for (auto& headComponent : headComponents) {
IntersectionComponents<Head, Tail...> currentEntityRequiredComponents;
if (allComponentsExist<IntersectionComponents<Head, Tail...>, Tail...>(headComponent.entityID, currentEntityRequiredComponents)) {
currentEntityRequiredComponents.set(headComponent);
results.push_back(std::move(currentEntityRequiredComponents));
}
}
return results;
}
template<typename IntersectComponents, typename Head, typename... Tail>
bool allComponentsExist(EntityID entityID, IntersectComponents& components) {
auto currentComponent = getComponent<Head>(entityID);
if (!currentComponent) {
return false;
}
if (allComponentsExist<IntersectComponents, Tail...>(entityID, components)) {
components.set(currentComponent);
return true;
}
return false;
}
It's not self contained, but I've tested IntersectionComponents class and it works. Code breaks on allComponentsExist call. It's template parameters probably need to be specified some other way...
This is definition of IntersectionComponents class, in case it would be helpful:
template<typename... ComponentTypes>
class IntersectionComponents {
public:
template<typename ComponentType>
ComponentType& get() {
return *getByType<ComponentType*>(components);
}
private:
std::tuple<ComponentTypes* ...> components;
template<typename ComponentType>
void set(ComponentType& component) {
getByType<ComponentType*>(components) = &component;
}
friend class ComponentsManager;
};
getByType function returns reference to object in a tuple with the specified type(in template parameter).
Here's the error list from compiler(completely incomprehensible for me ;/):
In file included from /usr/include/c++/5.2.0/functional:55:0,
from /usr/include/c++/5.2.0/memory:79,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:426,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/tuple: In instantiation of ‘struct std::tuple_element<1ul, std::tuple<BarComponent*> >’:
/usr/include/c++/5.2.0/tuple:755:12: required from ‘struct std::tuple_element<2ul, std::tuple<FooComponent*, BarComponent*> >’
/usr/include/c++/5.2.0/tuple:769:69: required by substitution of ‘template<long unsigned int __i, class _Tp> using __tuple_element_t = typename std::tuple_element::type [with long unsigned int __i = 2ul; _Tp = std::tuple<FooComponent*, BarComponent*>]’
/usr/include/c++/5.2.0/tuple:844:5: required by substitution of ‘template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_Elements ...> >&& std::get(std::tuple<_Elements ...>&&) [with long unsigned int __i = 2ul; _Elements = {FooComponent*, BarComponent*}]’
/mnt/data/dev/Active/ECS/src/utils/getTupleElementByID.h:31:103: required from ‘T& getByType(std::tuple<_Elements ...>&) [with T = BarComponent**; TupleElems = {FooComponent*, BarComponent*}]’
/mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:27:34: required from ‘void IntersectionComponents<ComponentTypes>::set(ComponentType&) [with ComponentType = BarComponent*; ComponentTypes = {FooComponent, BarComponent}]’
/mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:148:13: required from ‘bool ComponentsManager::allComponentsExist(EntityID, IntersectComponents&) [with IntersectComponents = IntersectionComponents<FooComponent, BarComponent>; Head = BarComponent; Tail = {}; EntityID = unsigned int]’
/mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:118:83: required from ‘std::vector<IntersectionComponents<Head, Tail ...> > ComponentsManager::intersection() [with Head = FooComponent; Tail = {BarComponent}]’
/mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:75:64: required from here
/usr/include/c++/5.2.0/tuple:755:12: error: invalid use of incomplete type ‘struct std::tuple_element<0ul, std::tuple<> >’
struct tuple_element<__i, tuple<_Head, _Tail...> >
^
In file included from /usr/include/c++/5.2.0/algorithm:60:0,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:67,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/utility:85:11: note: declaration of ‘struct std::tuple_element<0ul, std::tuple<> >’
class tuple_element;
^
In file included from /mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:4:0,
from /mnt/data/dev/Active/ECS/include/../src/core/engine.h:4,
from /mnt/data/dev/Active/ECS/./include/ecs/ecs.h:3,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:2:
/mnt/data/dev/Active/ECS/src/utils/getTupleElementByID.h: In instantiation of ‘T& getByType(std::tuple<_Elements ...>&) [with T = BarComponent**; TupleElems = {FooComponent*, BarComponent*}]’:
/mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:27:34: required from ‘void IntersectionComponents<ComponentTypes>::set(ComponentType&) [with ComponentType = BarComponent*; ComponentTypes = {FooComponent, BarComponent}]’
/mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:148:13: required from ‘bool ComponentsManager::allComponentsExist(EntityID, IntersectComponents&) [with IntersectComponents = IntersectionComponents<FooComponent, BarComponent>; Head = BarComponent; Tail = {}; EntityID = unsigned int]’
/mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:118:83: required from ‘std::vector<IntersectionComponents<Head, Tail ...> > ComponentsManager::intersection() [with Head = FooComponent; Tail = {BarComponent}]’
/mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:75:64: required from here
/mnt/data/dev/Active/ECS/src/utils/getTupleElementByID.h:31:103: error: no matching function for call to ‘get(std::tuple<FooComponent*, BarComponent*>&)’
return std::get<detail::get_number_of_element_from_tuple_by_type_impl<T, 0, TupleElems...>::value>(t);
^
In file included from /usr/include/c++/5.2.0/algorithm:60:0,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:67,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/utility:147:5: note: candidate: template<long unsigned int _Int, class _Tp1, class _Tp2> constexpr typename std::tuple_element<_Int, std::pair<_Tp1, _Tp2> >::type& std::get(std::pair<_Tp1, _Tp2>&)
get(std::pair<_Tp1, _Tp2>& __in) noexcept
^
/usr/include/c++/5.2.0/utility:147:5: note: template argument deduction/substitution failed:
In file included from /mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:4:0,
from /mnt/data/dev/Active/ECS/include/../src/core/engine.h:4,
from /mnt/data/dev/Active/ECS/./include/ecs/ecs.h:3,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:2:
/mnt/data/dev/Active/ECS/src/utils/getTupleElementByID.h:31:103: note: ‘std::tuple<FooComponent*, BarComponent*>’ is not derived from ‘std::pair<_Tp1, _Tp2>’
return std::get<detail::get_number_of_element_from_tuple_by_type_impl<T, 0, TupleElems...>::value>(t);
^
In file included from /usr/include/c++/5.2.0/algorithm:60:0,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:67,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/utility:152:5: note: candidate: template<long unsigned int _Int, class _Tp1, class _Tp2> constexpr typename std::tuple_element<_Int, std::pair<_Tp1, _Tp2> >::type&& std::get(std::pair<_Tp1, _Tp2>&&)
get(std::pair<_Tp1, _Tp2>&& __in) noexcept
^
/usr/include/c++/5.2.0/utility:152:5: note: template argument deduction/substitution failed:
In file included from /mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:4:0,
from /mnt/data/dev/Active/ECS/include/../src/core/engine.h:4,
from /mnt/data/dev/Active/ECS/./include/ecs/ecs.h:3,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:2:
/mnt/data/dev/Active/ECS/src/utils/getTupleElementByID.h:31:103: note: ‘std::tuple<FooComponent*, BarComponent*>’ is not derived from ‘std::pair<_Tp1, _Tp2>’
return std::get<detail::get_number_of_element_from_tuple_by_type_impl<T, 0, TupleElems...>::value>(t);
^
In file included from /usr/include/c++/5.2.0/algorithm:60:0,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:67,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/utility:157:5: note: candidate: template<long unsigned int _Int, class _Tp1, class _Tp2> constexpr const typename std::tuple_element<_Int, std::pair<_Tp1, _Tp2> >::type& std::get(const std::pair<_Tp1, _Tp2>&)
get(const std::pair<_Tp1, _Tp2>& __in) noexcept
^
/usr/include/c++/5.2.0/utility:157:5: note: template argument deduction/substitution failed:
In file included from /mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:4:0,
from /mnt/data/dev/Active/ECS/include/../src/core/engine.h:4,
from /mnt/data/dev/Active/ECS/./include/ecs/ecs.h:3,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:2:
/mnt/data/dev/Active/ECS/src/utils/getTupleElementByID.h:31:103: note: ‘std::tuple<FooComponent*, BarComponent*>’ is not derived from ‘const std::pair<_Tp1, _Tp2>’
return std::get<detail::get_number_of_element_from_tuple_by_type_impl<T, 0, TupleElems...>::value>(t);
^
In file included from /usr/include/c++/5.2.0/algorithm:60:0,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:67,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/utility:166:5: note: candidate: template<class _Tp, class _Up> constexpr _Tp& std::get(std::pair<_T1, _T2>&)
get(pair<_Tp, _Up>& __p) noexcept
^
/usr/include/c++/5.2.0/utility:166:5: note: template argument deduction/substitution failed:
/usr/include/c++/5.2.0/utility:171:5: note: candidate: template<class _Tp, class _Up> constexpr const _Tp& std::get(const std::pair<_T1, _T2>&)
get(const pair<_Tp, _Up>& __p) noexcept
^
/usr/include/c++/5.2.0/utility:171:5: note: template argument deduction/substitution failed:
/usr/include/c++/5.2.0/utility:176:5: note: candidate: template<class _Tp, class _Up> constexpr _Tp&& std::get(std::pair<_T1, _T2>&&)
get(pair<_Tp, _Up>&& __p) noexcept
^
/usr/include/c++/5.2.0/utility:176:5: note: template argument deduction/substitution failed:
/usr/include/c++/5.2.0/utility:181:5: note: candidate: template<class _Tp, class _Up> constexpr _Tp& std::get(std::pair<_Up, _Tp>&)
get(pair<_Up, _Tp>& __p) noexcept
^
/usr/include/c++/5.2.0/utility:181:5: note: template argument deduction/substitution failed:
/usr/include/c++/5.2.0/utility:186:5: note: candidate: template<class _Tp, class _Up> constexpr const _Tp& std::get(const std::pair<_Up, _Tp>&)
get(const pair<_Up, _Tp>& __p) noexcept
^
/usr/include/c++/5.2.0/utility:186:5: note: template argument deduction/substitution failed:
/usr/include/c++/5.2.0/utility:191:5: note: candidate: template<class _Tp, class _Up> constexpr _Tp&& std::get(std::pair<_Up, _Tp>&&)
get(pair<_Up, _Tp>&& __p) noexcept
^
/usr/include/c++/5.2.0/utility:191:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5.2.0/tuple:39:0,
from /usr/include/c++/5.2.0/functional:55,
from /usr/include/c++/5.2.0/memory:79,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:426,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/array:280:5: note: candidate: template<long unsigned int _Int, class _Tp, long unsigned int _Nm> constexpr _Tp& std::get(std::array<_Tp, _Nm>&)
get(array<_Tp, _Nm>& __arr) noexcept
^
/usr/include/c++/5.2.0/array:280:5: note: template argument deduction/substitution failed:
In file included from /mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:4:0,
from /mnt/data/dev/Active/ECS/include/../src/core/engine.h:4,
from /mnt/data/dev/Active/ECS/./include/ecs/ecs.h:3,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:2:
/mnt/data/dev/Active/ECS/src/utils/getTupleElementByID.h:31:103: note: ‘std::tuple<FooComponent*, BarComponent*>’ is not derived from ‘std::array<_Tp, _Nm>’
return std::get<detail::get_number_of_element_from_tuple_by_type_impl<T, 0, TupleElems...>::value>(t);
^
In file included from /usr/include/c++/5.2.0/tuple:39:0,
from /usr/include/c++/5.2.0/functional:55,
from /usr/include/c++/5.2.0/memory:79,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:426,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/array:289:5: note: candidate: template<long unsigned int _Int, class _Tp, long unsigned int _Nm> constexpr _Tp&& std::get(std::array<_Tp, _Nm>&&)
get(array<_Tp, _Nm>&& __arr) noexcept
^
/usr/include/c++/5.2.0/array:289:5: note: template argument deduction/substitution failed:
In file included from /mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:4:0,
from /mnt/data/dev/Active/ECS/include/../src/core/engine.h:4,
from /mnt/data/dev/Active/ECS/./include/ecs/ecs.h:3,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:2:
/mnt/data/dev/Active/ECS/src/utils/getTupleElementByID.h:31:103: note: ‘std::tuple<FooComponent*, BarComponent*>’ is not derived from ‘std::array<_Tp, _Nm>’
return std::get<detail::get_number_of_element_from_tuple_by_type_impl<T, 0, TupleElems...>::value>(t);
^
In file included from /usr/include/c++/5.2.0/tuple:39:0,
from /usr/include/c++/5.2.0/functional:55,
from /usr/include/c++/5.2.0/memory:79,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:426,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/array:297:5: note: candidate: template<long unsigned int _Int, class _Tp, long unsigned int _Nm> constexpr const _Tp& std::get(const std::array<_Tp, _Nm>&)
get(const array<_Tp, _Nm>& __arr) noexcept
^
/usr/include/c++/5.2.0/array:297:5: note: template argument deduction/substitution failed:
In file included from /mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:4:0,
from /mnt/data/dev/Active/ECS/include/../src/core/engine.h:4,
from /mnt/data/dev/Active/ECS/./include/ecs/ecs.h:3,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:2:
/mnt/data/dev/Active/ECS/src/utils/getTupleElementByID.h:31:103: note: ‘std::tuple<FooComponent*, BarComponent*>’ is not derived from ‘const std::array<_Tp, _Nm>’
return std::get<detail::get_number_of_element_from_tuple_by_type_impl<T, 0, TupleElems...>::value>(t);
^
In file included from /usr/include/c++/5.2.0/functional:55:0,
from /usr/include/c++/5.2.0/memory:79,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:426,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/tuple:832:5: note: candidate: template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_Elements ...> >& std::get(std::tuple<_Elements ...>&)
get(tuple<_Elements...>& __t) noexcept
^
/usr/include/c++/5.2.0/tuple:832:5: note: template argument deduction/substitution failed:
/usr/include/c++/5.2.0/tuple:838:5: note: candidate: template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_Elements ...> >& std::get(const std::tuple<_Elements ...>&)
get(const tuple<_Elements...>& __t) noexcept
^
/usr/include/c++/5.2.0/tuple:838:5: note: template argument deduction/substitution failed:
/usr/include/c++/5.2.0/tuple:844:5: note: candidate: template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_Elements ...> >&& std::get(std::tuple<_Elements ...>&&)
get(tuple<_Elements...>&& __t) noexcept
^
/usr/include/c++/5.2.0/tuple:844:5: note: substitution of deduced template arguments resulted in errors seen above
/usr/include/c++/5.2.0/tuple:867:5: note: candidate: template<class _Tp, class ... _Types> constexpr _Tp& std::get(std::tuple<_Elements ...>&)
get(tuple<_Types...>& __t) noexcept
^
/usr/include/c++/5.2.0/tuple:867:5: note: template argument deduction/substitution failed:
/usr/include/c++/5.2.0/tuple:873:5: note: candidate: template<class _Tp, class ... _Types> constexpr _Tp&& std::get(std::tuple<_Elements ...>&&)
get(tuple<_Types...>&& __t) noexcept
^
/usr/include/c++/5.2.0/tuple:873:5: note: template argument deduction/substitution failed:
/usr/include/c++/5.2.0/tuple:879:5: note: candidate: template<class _Tp, class ... _Types> constexpr const _Tp& std::get(const std::tuple<_Elements ...>&)
get(const tuple<_Types...>& __t) noexcept
^
/usr/include/c++/5.2.0/tuple:879:5: note: template argument deduction/substitution failed:
CMakeFiles/tests.dir/build.make:77: polecenia dla obiektu 'CMakeFiles/tests.dir/tests/core/componentsManagerTests.cpp.o' nie powiodły się

Solved it. Well, it took 3 minutes or so after I've got 'genius' idea to just look at all error locations reported instead of trying to comprehend actual errors. And I've searched for error in the wrong place, due to IDE which underlined following line in red:
allComponentsExist<IntersectionComponents<Head, Tail...>, Tail...>(headComponent.entityID, currentEntityRequiredComponents));
Actual problem was this line:
components.set(currentComponent);
set() should get reference to object, but currentComponent was a pointer... changed it to:
components.set(*currentComponent);
And wall of errors is gone.
This line of errors made me realize this:
...componentsManager.h:27:34: required from ‘void IntersectionComponents<ComponentTypes>::set(ComponentType&) [with ComponentType = BarComponent*; ComponentTypes = {FooComponent, BarComponent}]’
Specifically "with ComponentType = BarComponent*" part :S
Anyway, sorry for bad question.

Related

Can't "std::shared_from_this" be inherited by its derived classes? [duplicate]

This question already has answers here:
How to enable_shared_from_this of both parent and derived
(6 answers)
Closed 2 years ago.
Can't std::shared_from_this be inherited by its derived classes?
Why doesn't this code snippet compile(check http://cpp.sh/7llcr)? I have read the documentation(https://en.cppreference.com/w/cpp/memory/enable_shared_from_this) carefully, but still can't find any clue. I have thought about it for a long. I would be very grateful to have some help with this question.
Here is the code snippet which does not compile:
// enable_shared_from_this example
#include <iostream>
#include <memory>
struct C : std::enable_shared_from_this<C> {};
struct D : public C {};
int main () {
std::shared_ptr<D> foo, bar;
foo = std::make_shared<D>();
bar = foo->shared_from_this();
return 0;
}
Compiler complains:
In file included from /usr/include/c++/4.9/bits/shared_ptr.h:52:0,
from /usr/include/c++/4.9/memory:82,
from 3:
/usr/include/c++/4.9/bits/shared_ptr_base.h: In instantiation of 'std::__shared_ptr<_Tp, _Lp>& std::__shared_ptr<_Tp, _Lp>::operator=(std::__shared_ptr<_Tp1, _Lp>&&) [with _Tp1 = C; _Tp = D; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]':
/usr/include/c++/4.9/bits/shared_ptr.h:299:4: required from 'std::shared_ptr<_Tp>& std::shared_ptr<_Tp>::operator=(std::shared_ptr<_Tp1>&&) [with _Tp1 = C; _Tp = D]'
14:7: required from here
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: error: no matching function for call to 'std::__shared_ptr<D, (__gnu_cxx::_Lock_policy)2u>::__shared_ptr(std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type)'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: candidates are:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1145:7: note: std::__shared_ptr<_Tp, _Lp>::__shared_ptr(const std::__weak_ptr<_Tp, _Lp>&, std::nothrow_t) [with _Tp = D; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]
__shared_ptr(const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t)
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:1145:7: note: candidate expects 2 arguments, 1 provided
/usr/include/c++/4.9/bits/shared_ptr_base.h:1087:2: note: template<class _Alloc, class ... _Args> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...)
__shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:1087:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: cannot convert 'std::move<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>((* & __r))' (type 'std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type {aka std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>}') to type 'std::_Sp_make_shared_tag'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:967:17: note: constexpr std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::nullptr_t) [with _Tp = D; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u; std::nullptr_t = std::nullptr_t]
constexpr __shared_ptr(nullptr_t) noexcept
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:967:17: note: no known conversion for argument 1 from 'std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type {aka std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>}' to 'std::nullptr_t'
/usr/include/c++/4.9/bits/shared_ptr_base.h:963:2: note: template<class _Tp1> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::auto_ptr<_Up>&&)
__shared_ptr(std::auto_ptr<_Tp1>&& __r);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:963:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: 'std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type {aka std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>}' is not derived from 'std::auto_ptr<_Up>'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:951:2: note: template<class _Tp1, class _Del> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::unique_ptr<_Up, _Ep>&&)
__shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:951:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: 'std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type {aka std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>}' is not derived from 'std::unique_ptr<_Tp, _Dp>'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:939:11: note: template<class _Tp1> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(const std::__weak_ptr<_Tp1, _Lp>&)
explicit __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:939:11: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: 'std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type {aka std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>}' is not derived from 'const std::__weak_ptr<_Tp>'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:931:2: note: template<class _Tp1, class> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::__shared_ptr<_Tp1, _Lp>&&)
__shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:931:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:929:31: error: no type named 'type' in 'struct std::enable_if<false, void>'
template<typename _Tp1, typename = typename
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:922:7: note: std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::__shared_ptr<_Tp, _Lp>&&) [with _Tp = D; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]
__shared_ptr(__shared_ptr&& __r) noexcept
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:922:7: note: no known conversion for argument 1 from 'std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type {aka std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>}' to 'std::__shared_ptr<D, (__gnu_cxx::_Lock_policy)2u>&&'
/usr/include/c++/4.9/bits/shared_ptr_base.h:918:2: note: template<class _Tp1, class> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(const std::__shared_ptr<_Tp1, _Lp>&)
__shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:918:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:916:31: error: no type named 'type' in 'struct std::enable_if<false, void>'
template<typename _Tp1, typename = typename
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:912:7: note: std::__shared_ptr<_Tp, _Lp>::__shared_ptr(const std::__shared_ptr<_Tp, _Lp>&) [with _Tp = D; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]
__shared_ptr(const __shared_ptr&) noexcept = default;
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:912:7: note: no known conversion for argument 1 from 'std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type {aka std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>}' to 'const std::__shared_ptr<D, (__gnu_cxx::_Lock_policy)2u>&'
/usr/include/c++/4.9/bits/shared_ptr_base.h:908:2: note: template<class _Tp1> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(const std::__shared_ptr<_Tp1, _Lp>&, _Tp*)
__shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p) noexcept
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:908:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: candidate expects 2 arguments, 1 provided
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:903:9: note: template<class _Deleter, class _Alloc> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::nullptr_t, _Deleter, _Alloc)
__shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:903:9: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: cannot convert 'std::move<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>((* & __r))' (type 'std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type {aka std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>}') to type 'std::nullptr_t'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:898:2: note: template<class _Deleter> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::nullptr_t, _Deleter)
__shared_ptr(nullptr_t __p, _Deleter __d)
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:898:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: cannot convert 'std::move<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>((* & __r))' (type 'std::remove_reference<std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>&>::type {aka std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>}') to type 'std::nullptr_t'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:889:2: note: template<class _Tp1, class _Deleter, class _Alloc> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(_Tp1*, _Deleter, _Alloc)
__shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:889:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: mismatched types '_Tp1*' and 'std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:880:2: note: template<class _Tp1, class _Deleter> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(_Tp1*, _Deleter)
__shared_ptr(_Tp1* __p, _Deleter __d)
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:880:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: mismatched types '_Tp1*' and 'std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:870:11: note: template<class _Tp1> std::__shared_ptr<_Tp, _Lp>::__shared_ptr(_Tp1*)
explicit __shared_ptr(_Tp1* __p)
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:870:11: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/shared_ptr_base.h:1001:4: note: mismatched types '_Tp1*' and 'std::__shared_ptr<C, (__gnu_cxx::_Lock_policy)2u>'
__shared_ptr(std::move(__r)).swap(*this);
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:865:17: note: constexpr std::__shared_ptr<_Tp, _Lp>::__shared_ptr() [with _Tp = D; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]
constexpr __shared_ptr() noexcept
^
/usr/include/c++/4.9/bits/shared_ptr_base.h:865:17: note: candidate expects 0 arguments, 1 provided
It is inherited:
// enable_shared_from_this example
#include <iostream>
#include <memory>
struct C : std::enable_shared_from_this<C> {};
struct D : public C {};
int main () {
std::shared_ptr<D> foo;
foo = std::make_shared<D>();
std::shared_ptr<C> bar = foo->shared_from_this();
return 0;
}
You define the shared_from_this in the class C: so the object returns std::shated_ptr<C>. No surprise. You still may downcast it to std::shared_ptr<D> with std::dynamic_pointer_cast.
As #DimityKuzminov already pointed out, shared_from_this() will always return a std::shared_pointer<C> also when called from std::shared_ptr<D>. In order to use std::dynmaic_pointer_cast your base also needs to have a virtual function like a virtual destructor (otherwise you compiler will tell you source type is not polymorphic). Here is an example:
// enable_shared_from_this example
#include <iostream>
#include <memory>
struct C : std::enable_shared_from_this<C> {
virtual ~C() = default;
};
template<typename T, typename U>
std::shared_ptr<T>
shared_from(const std::shared_ptr<U> &a)
{
return std::dynamic_pointer_cast<T>(a->shared_from_this());
}
struct D : public C {};
int main () {
std::shared_ptr<D> foo, bar;
foo = std::make_shared<D>();
bar = shared_from<D>(foo);
return 0;
}

How to compile this example with async_read_until, async_write and Boost.Asio?

Wrote short example: simple echo program. Please, help to compile this, because I don't understand why compiler error
I tried this: g++ -Wall -Wextra -pedantic client.cpp -o client -lboost_thread -lboost_system, but got next error https://pastebin.com/byevYxPa
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <string>
using namespace boost::asio;
using error_code = boost::system::error_code;
io_service ioservice;
posix::stream_descriptor out(ioservice, STDOUT_FILENO);
posix::stream_descriptor in(ioservice, STDIN_FILENO);
std::string line;
void on_read(const error_code & err, std::size_t bytes);
void on_write(const error_code & err, std::size_t bytes);
void on_read(const error_code & err, std::size_t bytes) {
if (err || line == "exit") return;
line += "\n";
async_write(out, buffer(line), on_write);
}
void on_write(const error_code & err, std::size_t bytes) {
write(out, buffer("$ "));
async_read_until(in, buffer(line),'\n',on_read);
}
int main() {
async_read_until(in, buffer(line),'\n',on_read);
ioservice.run();
}
I expected that this code will works correctly and will be compiled
client.cpp: In function ‘void on_read(const error_code&, std::size_t)’:
client.cpp:17:50: warning: unused parameter ‘bytes’ [-Wunused-parameter]
void on_read(const error_code & err, std::size_t bytes) {
^~~~~
client.cpp: In function ‘void on_write(const error_code&, std::size_t)’:
client.cpp:25:51: error: no matching function for call to ‘async_read_until(boost::asio::posix::stream_descriptor&, boost::asio::const_buffers_1, char, void (&)(const error_code&, std::size_t))’
async_read_until(in, buffer(line),'\n',on_read);
^
In file included from /usr/include/boost/asio/read_until.hpp:921:0,
from /usr/include/boost/asio.hpp:91,
from client.cpp:1:
/usr/include/boost/asio/impl/read_until.hpp:498:1: note: candidate: template<class AsyncReadStream, class Allocator, class ReadHandler> typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::async_read_until(AsyncReadStream&, boost::asio::basic_streambuf<Allocator>&, char, ReadHandler&&)
async_read_until(AsyncReadStream& s,
^~~~~~~~~~~~~~~~
/usr/include/boost/asio/impl/read_until.hpp:498:1: note: template argument deduction/substitution failed:
client.cpp:25:51: note: ‘boost::asio::const_buffers_1’ is not derived from ‘boost::asio::basic_streambuf<Allocator>’
async_read_until(in, buffer(line),'\n',on_read);
^
In file included from /usr/include/boost/asio/read_until.hpp:921:0,
from /usr/include/boost/asio.hpp:91,
from client.cpp:1:
/usr/include/boost/asio/impl/read_until.hpp:701:1: note: candidate: template<class AsyncReadStream, class Allocator, class ReadHandler> typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::async_read_until(AsyncReadStream&, boost::asio::basic_streambuf<Allocator>&, const string&, ReadHandler&&)
async_read_until(AsyncReadStream& s,
^~~~~~~~~~~~~~~~
/usr/include/boost/asio/impl/read_until.hpp:701:1: note: template argument deduction/substitution failed:
client.cpp:25:51: note: ‘boost::asio::const_buffers_1’ is not derived from ‘boost::asio::basic_streambuf<Allocator>’
async_read_until(in, buffer(line),'\n',on_read);
^
In file included from /usr/include/boost/asio/read_until.hpp:921:0,
from /usr/include/boost/asio.hpp:91,
from client.cpp:1:
/usr/include/boost/asio/impl/read_until.hpp:913:1: note: candidate: template<class AsyncReadStream, class Allocator, class ReadHandler> typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::async_read_until(AsyncReadStream&, boost::asio::basic_streambuf<Allocator>&, const regex&, ReadHandler&&)
async_read_until(AsyncReadStream& s,
^~~~~~~~~~~~~~~~
/usr/include/boost/asio/impl/read_until.hpp:913:1: note: template argument deduction/substitution failed:
client.cpp:25:51: note: ‘boost::asio::const_buffers_1’ is not derived from ‘boost::asio::basic_streambuf<Allocator>’
async_read_until(in, buffer(line),'\n',on_read);
^
In file included from /usr/include/boost/asio/read_until.hpp:921:0,
from /usr/include/boost/asio.hpp:91,
from client.cpp:1:
/usr/include/boost/asio/impl/read_until.hpp:1122:1: note: candidate: template<class AsyncReadStream, class Allocator, class MatchCondition, class ReadHandler> typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::async_read_until(AsyncReadStream&, boost::asio::basic_streambuf<Allocator>&, MatchCondition, ReadHandler&&, typename std::enable_if<boost::asio::is_match_condition<MatchCondition>::value>::type*)
async_read_until(AsyncReadStream& s,
^~~~~~~~~~~~~~~~
/usr/include/boost/asio/impl/read_until.hpp:1122:1: note: template argument deduction/substitution failed:
client.cpp:25:51: note: ‘boost::asio::const_buffers_1’ is not derived from ‘boost::asio::basic_streambuf<Allocator>’
async_read_until(in, buffer(line),'\n',on_read);
^
client.cpp:23:34: warning: unused parameter ‘err’ [-Wunused-parameter]
void on_write(const error_code & err, std::size_t bytes) {
^~~
client.cpp:23:51: warning: unused parameter ‘bytes’ [-Wunused-parameter]
void on_write(const error_code & err, std::size_t bytes) {
^~~~~
client.cpp: In function ‘int main()’:
client.cpp:29:51: error: no matching function for call to ‘async_read_until(boost::asio::posix::stream_descriptor&, boost::asio::const_buffers_1, char, void (&)(const error_code&, std::size_t))’
async_read_until(in, buffer(line),'\n',on_read);
^
In file included from /usr/include/boost/asio/read_until.hpp:921:0,
from /usr/include/boost/asio.hpp:91,
from client.cpp:1:
/usr/include/boost/asio/impl/read_until.hpp:498:1: note: candidate: template<class AsyncReadStream, class Allocator, class ReadHandler> typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::async_read_until(AsyncReadStream&, boost::asio::basic_streambuf<Allocator>&, char, ReadHandler&&)
async_read_until(AsyncReadStream& s,
^~~~~~~~~~~~~~~~
/usr/include/boost/asio/impl/read_until.hpp:498:1: note: template argument deduction/substitution failed:
client.cpp:29:51: note: ‘boost::asio::const_buffers_1’ is not derived from ‘boost::asio::basic_streambuf<Allocator>’
async_read_until(in, buffer(line),'\n',on_read);
^
In file included from /usr/include/boost/asio/read_until.hpp:921:0,
from /usr/include/boost/asio.hpp:91,
from client.cpp:1:
/usr/include/boost/asio/impl/read_until.hpp:701:1: note: candidate: template<class AsyncReadStream, class Allocator, class ReadHandler> typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::async_read_until(AsyncReadStream&, boost::asio::basic_streambuf<Allocator>&, const string&, ReadHandler&&)
async_read_until(AsyncReadStream& s,
^~~~~~~~~~~~~~~~
/usr/include/boost/asio/impl/read_until.hpp:701:1: note: template argument deduction/substitution failed:
client.cpp:29:51: note: ‘boost::asio::const_buffers_1’ is not derived from ‘boost::asio::basic_streambuf<Allocator>’
async_read_until(in, buffer(line),'\n',on_read);
^
In file included from /usr/include/boost/asio/read_until.hpp:921:0,
from /usr/include/boost/asio.hpp:91,
from client.cpp:1:
/usr/include/boost/asio/impl/read_until.hpp:913:1: note: candidate: template<class AsyncReadStream, class Allocator, class ReadHandler> typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::async_read_until(AsyncReadStream&, boost::asio::basic_streambuf<Allocator>&, const regex&, ReadHandler&&)
async_read_until(AsyncReadStream& s,
^~~~~~~~~~~~~~~~
/usr/include/boost/asio/impl/read_until.hpp:913:1: note: template argument deduction/substitution failed:
client.cpp:29:51: note: ‘boost::asio::const_buffers_1’ is not derived from ‘boost::asio::basic_streambuf<Allocator>’
async_read_until(in, buffer(line),'\n',on_read);
^
In file included from /usr/include/boost/asio/read_until.hpp:921:0,
from /usr/include/boost/asio.hpp:91,
from client.cpp:1:
/usr/include/boost/asio/impl/read_until.hpp:1122:1: note: candidate: template<class AsyncReadStream, class Allocator, class MatchCondition, class ReadHandler> typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::async_read_until(AsyncReadStream&, boost::asio::basic_streambuf<Allocator>&, MatchCondition, ReadHandler&&, typename std::enable_if<boost::asio::is_match_condition<MatchCondition>::value>::type*)
async_read_until(AsyncReadStream& s,
^~~~~~~~~~~~~~~~
/usr/include/boost/asio/impl/read_until.hpp:1122:1: note: template argument deduction/substitution failed:
client.cpp:29:51: note: ‘boost::asio::const_buffers_1’ is not derived from ‘boost::asio::basic_streambuf<Allocator>’
async_read_until(in, buffer(line),'\n',on_read);
^
asio::buffer returns mutable buffer type, it doesn't match to the second param of async_read_until. This overload takes dynamic buffer, just use boost::asio::dynamic_buffer.
void on_write(const error_code & err, std::size_t bytes) {
write(out, buffer("$ "));
async_read_until(in, dynamic_buffer(line),'\n',on_read);
}
int main() {
async_read_until(in, dynamic_buffer(line),'\n',on_read);
ioservice.run();
}
LIVE demo

Unique_ptr c++ usage

Sincerelly i m not so expert with c++ RAII features. I never used before. Anyway i m begining to study about it (i m in "kernel panic"). Compiling a module i have the following error :
In file included from /usr/include/c++/6/memory:81:0,
from ./java/./rocksjni/compaction_filter_factory_jnicallback.h:13,
from java/rocksjni/compaction_filter_factory_jnicallback.cc:9:
/usr/include/c++/6/bits/unique_ptr.h: In instantiation of ‘void std::unique_ptr<_Tp [], _Dp>::reset(_Up) [with _Up = char*; <template-parameter-2-2> = void; _Tp = const char; _Dp = std::default_delete<const char []>]’:
/usr/include/c++/6/bits/unique_ptr.h:539:9: required from ‘typename std::enable_if<std::__and_<std::__and_<std::is_array<_Up>, std::is_same<typename std::unique_ptr<_Tp [], _Dp>::_Pointer::type, _Tp*>, std::is_same<typename std::unique_ptr<_Up, _Ep>::pointer, typename std::unique_ptr<_Up, _Ep>::element_type*>, std::is_convertible<typename std::unique_ptr<_Up, _Ep>::element_type (*)[], _Tp (*)[]>, std::__or_<std::__and_<std::is_reference<_Dp>, std::is_same<_T2, _U2> >, std::__and_<std::__not_<std::is_reference<_Dp> >, std::is_convertible<_Ep, _Dp> > > >, std::is_assignable<_T2&, _U2&&> >::value, std::unique_ptr<_Tp [], _Dp>&>::type std::unique_ptr<_Tp [], _Dp>::operator=(std::unique_ptr<_Up, _Ep>&&) [with _Up = char []; _Ep = std::default_delete<char []>; _Tp = const char; _Dp = std::default_delete<const char []>; typename std::enable_if<std::__and_<std::__and_<std::is_array<_Up>, std::is_same<typename std::unique_ptr<_Tp [], _Dp>::_Pointer::type, _Tp*>, std::is_same<typename std::unique_ptr<_Up, _Ep>::pointer, typename std::unique_ptr<_Up, _Ep>::element_type*>, std::is_convertible<typename std::unique_ptr<_Up, _Ep>::element_type (*)[], _Tp (*)[]>, std::__or_<std::__and_<std::is_reference<_Dp>, std::is_same<_T2, _U2> >, std::__and_<std::__not_<std::is_reference<_Dp> >, std::is_convertible<_Ep, _Dp> > > >, std::is_assignable<_T2&, _U2&&> >::value, std::unique_ptr<_Tp [], _Dp>&>::type = std::unique_ptr<const char []>&]’
java/rocksjni/compaction_filter_factory_jnicallback.cc:33:58: required from here
/usr/include/c++/6/bits/unique_ptr.h:614:6: error: no matching function for call to ‘swap(const char*&, char*&)’
swap(std::get<0>(_M_t), __p);
~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/bits/stl_pair.h:59:0,
from /usr/include/c++/6/bits/stl_algobase.h:64,
from /usr/include/c++/6/memory:62,
from ./java/./rocksjni/compaction_filter_factory_jnicallback.h:13,
from java/rocksjni/compaction_filter_factory_jnicallback.cc:9:
/usr/include/c++/6/bits/move.h:179:5: note: candidate: template<class _Tp> typename std::enable_if<std::__and_<std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp> >::value>::type std::swap(_Tp&, _Tp&)
swap(_Tp& __a, _Tp& __b)
^~~~
/usr/include/c++/6/bits/move.h:179:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/6/memory:81:0,
from ./java/./rocksjni/compaction_filter_factory_jnicallback.h:13,
from java/rocksjni/compaction_filter_factory_jnicallback.cc:9:
/usr/include/c++/6/bits/unique_ptr.h:614:6: note: deduced conflicting types for parameter ‘_Tp’ (‘const char*’ and ‘char*’)
swap(std::get<0>(_M_t), __p);
~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/bits/stl_pair.h:59:0,
from /usr/include/c++/6/bits/stl_algobase.h:64,
from /usr/include/c++/6/memory:62,
from ./java/./rocksjni/compaction_filter_factory_jnicallback.h:13,
from java/rocksjni/compaction_filter_factory_jnicallback.cc:9:
/usr/include/c++/6/bits/move.h:202:5: note: candidate: template<class _Tp, long unsigned int _Nm> typename std::enable_if<std::__is_swappable<_Tp>::value>::type std::swap(_Tp (&)[_Nm], _Tp (&)[_Nm])
swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
^~~~
/usr/include/c++/6/bits/move.h:202:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/6/memory:81:0,
from ./java/./rocksjni/compaction_filter_factory_jnicallback.h:13,
from java/rocksjni/compaction_filter_factory_jnicallback.cc:9:
/usr/include/c++/6/bits/unique_ptr.h:614:6: note: mismatched types ‘_Tp [_Nm]’ and ‘std::__tuple_element_t<0ul, std::tuple<const char*, std::default_delete<const char []> > > {aka const char*}’
swap(std::get<0>(_M_t), __p);
~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/bits/stl_algobase.h:64:0,
from /usr/include/c++/6/memory:62,
from ./java/./rocksjni/compaction_filter_factory_jnicallback.h:13,
from java/rocksjni/compaction_filter_factory_jnicallback.cc:9:
/usr/include/c++/6/bits/stl_pair.h:471:5: note: candidate: template<class _T1, class _T2> void std::swap(std::pair<_T1, _T2>&, std::pair<_T1, _T2>&)
swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
^~~~
/usr/include/c++/6/bits/stl_pair.h:471:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/6/memory:81:0,
from ./java/./rocksjni/compaction_filter_factory_jnicallback.h:13,
from java/rocksjni/compaction_filter_factory_jnicallback.cc:9:
/usr/include/c++/6/bits/unique_ptr.h:614:6: note: mismatched types ‘std::pair<_T1, _T2>’ and ‘std::__tuple_element_t<0ul, std::tuple<const char*, std::default_delete<const char []> > > {aka const char*}’
swap(std::get<0>(_M_t), __p);
~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/string:52:0,
from /usr/include/c++/6/stdexcept:39,
from /usr/include/c++/6/array:39,
from /usr/include/c++/6/tuple:39,
from /usr/include/c++/6/functional:55,
from /usr/include/c++/6/memory:79,
from ./java/./rocksjni/compaction_filter_factory_jnicallback.h:13,
from java/rocksjni/compaction_filter_factory_jnicallback.cc:9:
/usr/include/c++/6/bits/basic_string.h:5287:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> void std::swap(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
^~~~
/usr/include/c++/6/bits/basic_string.h:5287:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/6/memory:81:0,
from ./java/./rocksjni/compaction_filter_factory_jnicallback.h:13,
from java/rocksjni/compaction_filter_factory_jnicallback.cc:9:
/usr/include/c++/6/bits/unique_ptr.h:614:6: note: mismatched types ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’ and ‘std::__tuple_element_t<0ul, std::tuple<const char*, std::default_delete<const char []> > > {aka const char*}’
swap(std::get<0>(_M_t), __p);
~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/tuple:39:0,
from /usr/include/c++/6/functional:55,
from /usr/include/c++/6/memory:79,
from ./java/./rocksjni/compaction_filter_factory_jnicallback.h:13,
from java/rocksjni/compaction_filter_factory_jnicallback.cc:9:
/usr/include/c++/6/array:275:5: note: candidate: template<class _Tp, long unsigned int _Nm> void std::swap(std::array<_Tp, _Nm>&, std::array<_Tp, _Nm>&)
swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
^~~~
/usr/include/c++/6/array:275:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/6/memory:81:0,
from ./java/./rocksjni/compaction_filter_factory_jnicallback.h:13,
from java/rocksjni/compaction_filter_factory_jnicallback.cc:9:
/usr/include/c++/6/bits/unique_ptr.h:614:6: note: mismatched types ‘std::array<_Tp, _Nm>’ and ‘std::__tuple_element_t<0ul, std::tuple<const char*, std::default_delete<const char []> > > {aka const char*}’
swap(std::get<0>(_M_t), __p);
~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/functional:55:0,
from /usr/include/c++/6/memory:79,
from ./java/./rocksjni/compaction_filter_factory_jnicallback.h:13,
from java/rocksjni/compaction_filter_factory_jnicallback.cc:9:
/usr/include/c++/6/tuple:1546:5: note: candidate: template<class ... _Elements> void std::swap(std::tuple<_Elements ...>&, std::tuple<_Elements ...>&)
swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
^~~~
/usr/include/c++/6/tuple:1546:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/6/memory:81:0,
from ./java/./rocksjni/compaction_filter_factory_jnicallback.h:13,
from java/rocksjni/compaction_filter_factory_jnicallback.cc:9:
/usr/include/c++/6/bits/unique_ptr.h:614:6: note: mismatched types ‘std::tuple<_Elements ...>’ and ‘std::__tuple_element_t<0ul, std::tuple<const char*, std::default_delete<const char []> > > {aka const char*}’
swap(std::get<0>(_M_t), __p);
~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/memory:79:0,
from ./java/./rocksjni/compaction_filter_factory_jnicallback.h:13,
from java/rocksjni/compaction_filter_factory_jnicallback.cc:9:
/usr/include/c++/6/functional:2238:5: note: candidate: template<class _Res, class ... _Args> void std::swap(std::function<_Res(_ArgTypes ...)>&, std::function<_Res(_ArgTypes ...)>&)
swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
^~~~
/usr/include/c++/6/functional:2238:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/6/memory:81:0,
from ./java/./rocksjni/compaction_filter_factory_jnicallback.h:13,
from java/rocksjni/compaction_filter_factory_jnicallback.cc:9:
/usr/include/c++/6/bits/unique_ptr.h:614:6: note: mismatched types ‘std::function<_Res(_ArgTypes ...)>’ and ‘std::__tuple_element_t<0ul, std::tuple<const char*, std::default_delete<const char []> > > {aka const char*}’
swap(std::get<0>(_M_t), __p);
~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/memory:81:0,
from ./java/./rocksjni/comparatorjnicallback.h:13,
from java/rocksjni/comparatorjnicallback.cc:9:
/usr/include/c++/6/bits/unique_ptr.h: In instantiation of ‘void std::unique_ptr<_Tp [], _Dp>::reset(_Up) [with _Up = char*; <template-parameter-2-2> = void; _Tp = const char; _Dp = std::default_delete<const char []>]’:
/usr/include/c++/6/bits/unique_ptr.h:539:9: required from ‘typename std::enable_if<std::__and_<std::__and_<std::is_array<_Up>, std::is_same<typename std::unique_ptr<_Tp [], _Dp>::_Pointer::type, _Tp*>, std::is_same<typename std::unique_ptr<_Up, _Ep>::pointer, typename std::unique_ptr<_Up, _Ep>::element_type*>, std::is_convertible<typename std::unique_ptr<_Up, _Ep>::element_type (*)[], _Tp (*)[]>, std::__or_<std::__and_<std::is_reference<_Dp>, std::is_same<_T2, _U2> >, std::__and_<std::__not_<std::is_reference<_Dp> >, std::is_convertible<_Ep, _Dp> > > >, std::is_assignable<_T2&, _U2&&> >::value, std::unique_ptr<_Tp [], _Dp>&>::type std::unique_ptr<_Tp [], _Dp>::operator=(std::unique_ptr<_Up, _Ep>&&) [with _Up = char []; _Ep = std::default_delete<char []>; _Tp = const char; _Dp = std::default_delete<const char []>; typename std::enable_if<std::__and_<std::__and_<std::is_array<_Up>, std::is_same<typename std::unique_ptr<_Tp [], _Dp>::_Pointer::type, _Tp*>, std::is_same<typename std::unique_ptr<_Up, _Ep>::pointer, typename std::unique_ptr<_Up, _Ep>::element_type*>, std::is_convertible<typename std::unique_ptr<_Up, _Ep>::element_type (*)[], _Tp (*)[]>, std::__or_<std::__and_<std::is_reference<_Dp>, std::is_same<_T2, _U2> >, std::__and_<std::__not_<std::is_reference<_Dp> >, std::is_convertible<_Ep, _Dp> > > >, std::is_assignable<_T2&, _U2&&> >::value, std::unique_ptr<_Tp [], _Dp>&>::type = std::unique_ptr<const char []>&]’
java/rocksjni/comparatorjnicallback.cc:34:21: required from here
/usr/include/c++/6/bits/unique_ptr.h:614:6: error: no matching function for call to ‘swap(const char*&, char*&)’
swap(std::get<0>(_M_t), __p);
~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/bits/stl_pair.h:59:0,
from /usr/include/c++/6/bits/stl_algobase.h:64,
from /usr/include/c++/6/memory:62,
from ./java/./rocksjni/comparatorjnicallback.h:13,
from java/rocksjni/comparatorjnicallback.cc:9:
/usr/include/c++/6/bits/move.h:179:5: note: candidate: template<class _Tp> typename std::enable_if<std::__and_<std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp> >::value>::type std::swap(_Tp&, _Tp&)
swap(_Tp& __a, _Tp& __b)
^~~~
/usr/include/c++/6/bits/move.h:179:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/6/memory:81:0,
from ./java/./rocksjni/comparatorjnicallback.h:13,
from java/rocksjni/comparatorjnicallback.cc:9:
/usr/include/c++/6/bits/unique_ptr.h:614:6: note: deduced conflicting types for parameter ‘_Tp’ (‘const char*’ and ‘char*’)
swap(std::get<0>(_M_t), __p);
~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/bits/stl_pair.h:59:0,
from /usr/include/c++/6/bits/stl_algobase.h:64,
from /usr/include/c++/6/memory:62,
from ./java/./rocksjni/comparatorjnicallback.h:13,
from java/rocksjni/comparatorjnicallback.cc:9:
/usr/include/c++/6/bits/move.h:202:5: note: candidate: template<class _Tp, long unsigned int _Nm> typename std::enable_if<std::__is_swappable<_Tp>::value>::type std::swap(_Tp (&)[_Nm], _Tp (&)[_Nm])
swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
^~~~
/usr/include/c++/6/bits/move.h:202:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/6/memory:81:0,
from ./java/./rocksjni/comparatorjnicallback.h:13,
from java/rocksjni/comparatorjnicallback.cc:9:
/usr/include/c++/6/bits/unique_ptr.h:614:6: note: mismatched types ‘_Tp [_Nm]’ and ‘std::__tuple_element_t<0ul, std::tuple<const char*, std::default_delete<const char []> > > {aka const char*}’
swap(std::get<0>(_M_t), __p);
~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/bits/stl_algobase.h:64:0,
from /usr/include/c++/6/memory:62,
from ./java/./rocksjni/comparatorjnicallback.h:13,
from java/rocksjni/comparatorjnicallback.cc:9:
/usr/include/c++/6/bits/stl_pair.h:471:5: note: candidate: template<class _T1, class _T2> void std::swap(std::pair<_T1, _T2>&, std::pair<_T1, _T2>&)
swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
^~~~
/usr/include/c++/6/bits/stl_pair.h:471:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/6/memory:81:0,
from ./java/./rocksjni/comparatorjnicallback.h:13,
from java/rocksjni/comparatorjnicallback.cc:9:
/usr/include/c++/6/bits/unique_ptr.h:614:6: note: mismatched types ‘std::pair<_T1, _T2>’ and ‘std::__tuple_element_t<0ul, std::tuple<const char*, std::default_delete<const char []> > > {aka const char*}’
swap(std::get<0>(_M_t), __p);
~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/string:52:0,
from /usr/include/c++/6/stdexcept:39,
from /usr/include/c++/6/array:39,
from /usr/include/c++/6/tuple:39,
from /usr/include/c++/6/functional:55,
from /usr/include/c++/6/memory:79,
from ./java/./rocksjni/comparatorjnicallback.h:13,
from java/rocksjni/comparatorjnicallback.cc:9:
/usr/include/c++/6/bits/basic_string.h:5287:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> void std::swap(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
^~~~
/usr/include/c++/6/bits/basic_string.h:5287:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/6/memory:81:0,
from ./java/./rocksjni/comparatorjnicallback.h:13,
from java/rocksjni/comparatorjnicallback.cc:9:
/usr/include/c++/6/bits/unique_ptr.h:614:6: note: mismatched types ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’ and ‘std::__tuple_element_t<0ul, std::tuple<const char*, std::default_delete<const char []> > > {aka const char*}’
swap(std::get<0>(_M_t), __p);
~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/tuple:39:0,
from /usr/include/c++/6/functional:55,
from /usr/include/c++/6/memory:79,
from ./java/./rocksjni/comparatorjnicallback.h:13,
from java/rocksjni/comparatorjnicallback.cc:9:
/usr/include/c++/6/array:275:5: note: candidate: template<class _Tp, long unsigned int _Nm> void std::swap(std::array<_Tp, _Nm>&, std::array<_Tp, _Nm>&)
swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
^~~~
/usr/include/c++/6/array:275:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/6/memory:81:0,
from ./java/./rocksjni/comparatorjnicallback.h:13,
from java/rocksjni/comparatorjnicallback.cc:9:
/usr/include/c++/6/bits/unique_ptr.h:614:6: note: mismatched types ‘std::array<_Tp, _Nm>’ and ‘std::__tuple_element_t<0ul, std::tuple<const char*, std::default_delete<const char []> > > {aka const char*}’
swap(std::get<0>(_M_t), __p);
~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/functional:55:0,
from /usr/include/c++/6/memory:79,
from ./java/./rocksjni/comparatorjnicallback.h:13,
from java/rocksjni/comparatorjnicallback.cc:9:
/usr/include/c++/6/tuple:1546:5: note: candidate: template<class ... _Elements> void std::swap(std::tuple<_Elements ...>&, std::tuple<_Elements ...>&)
swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
^~~~
/usr/include/c++/6/tuple:1546:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/6/memory:81:0,
from ./java/./rocksjni/comparatorjnicallback.h:13,
from java/rocksjni/comparatorjnicallback.cc:9:
/usr/include/c++/6/bits/unique_ptr.h:614:6: note: mismatched types ‘std::tuple<_Elements ...>’ and ‘std::__tuple_element_t<0ul, std::tuple<const char*, std::default_delete<const char []> > > {aka const char*}’
swap(std::get<0>(_M_t), __p);
~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/memory:79:0,
from ./java/./rocksjni/comparatorjnicallback.h:13,
from java/rocksjni/comparatorjnicallback.cc:9:
/usr/include/c++/6/functional:2238:5: note: candidate: template<class _Res, class ... _Args> void std::swap(std::function<_Res(_ArgTypes ...)>&, std::function<_Res(_ArgTypes ...)>&)
swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
^~~~
/usr/include/c++/6/functional:2238:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/6/memory:81:0,
from ./java/./rocksjni/comparatorjnicallback.h:13,
from java/rocksjni/comparatorjnicallback.cc:9:
/usr/include/c++/6/bits/unique_ptr.h:614:6: note: mismatched types ‘std::function<_Res(_ArgTypes ...)>’ and ‘std::__tuple_element_t<0ul, std::tuple<const char*, std::default_delete<const char []> > > {aka const char*}’
swap(std::get<0>(_M_t), __p);
I m not sure to understand this stack trace but it seams there is a mismatch in the unique_ptr . Maybe const char* and char*.
the class where there is the bug is :
CompactionFilterFactoryJniCallback::CompactionFilterFactoryJniCallback(
JNIEnv* env, jobject jcompaction_filter_factory)
: JniCallback(env, jcompaction_filter_factory) {
// Note: The name of a CompactionFilterFactory will not change during
// it's lifetime, so we cache it in a global var
jmethodID jname_method_id =
AbstractCompactionFilterFactoryJni::getNameMethodId(env);
if(jname_method_id == nullptr) {
// exception thrown: NoSuchMethodException or OutOfMemoryError
return;
}
jstring jname =
(jstring)env->CallObjectMethod(m_jcallback_obj, jname_method_id);
if(env->ExceptionCheck()) {
// exception thrown
return;
}
jboolean has_exception = JNI_FALSE;
// line 33
m_name = JniUtil::copyString(env, jname, &has_exception); // also releases jname
if (has_exception == JNI_TRUE) {
// exception thrown
return;
}
m_jcreate_compaction_filter_methodid =
AbstractCompactionFilterFactoryJni::getCreateCompactionFilterMethodId(env);
if(m_jcreate_compaction_filter_methodid == nullptr) {
// exception thrown: NoSuchMethodException or OutOfMemoryError
return;
}
}
const char* CompactionFilterFactoryJniCallback::Name() const {
return m_name.get();
}
std::unique_ptr<CompactionFilter> CompactionFilterFactoryJniCallback::CreateCompactionFilter(
const CompactionFilter::Context& context) {
jboolean attached_thread = JNI_FALSE;
JNIEnv* env = getJniEnv(&attached_thread);
assert(env != nullptr);
jlong addr_compaction_filter = env->CallLongMethod(m_jcallback_obj,
m_jcreate_compaction_filter_methodid,
static_cast<jboolean>(context.is_full_compaction),
static_cast<jboolean>(context.is_manual_compaction));
if(env->ExceptionCheck()) {
// exception thrown from CallLongMethod
env->ExceptionDescribe(); // print out exception to stderr
releaseJniEnv(attached_thread);
return nullptr;
}
auto* const cff = reinterpret_cast<CompactionFilter*>(addr_compaction_filter);
releaseJniEnv(attached_thread);
return std::unique_ptr<CompactionFilter>(cff);
}
It is not clear how to read this stack trace for identifying the problem. I would appreciate to receive a little suggestion for understand how fix the problem and read the stack trace.
The error says that you have a std::unique_ptr<char const[]> object on which you try to call function reset passing char* as an argument.
Well, that argument needs to be char const* for that to compile, which you can achieve with a const_cast, e.g.:
std::unique_ptr<char const[]> p;
char* q = new char[1]{};
p.reset(const_cast<char const*>(q));
Looks like JniUtil::copyString returns std::unique_ptr<char[]> and you assign it to std::unique_ptr<char const[]> which causes the error.
One fix is to change the type of m_name to be std::unique_ptr<char[]>.
Another is to manually add constness (adding constness is safe and is an implicit conversion, but is needed here to choose the correct overload of std::unique_ptr<>::reset):
m_name.reset(const_cast<char const*>(JniUtil::copyString(...).release()));

Uniform Initialization with curly brace is mistaken as Initializer List

I have a class as:
#include <memory>
class Object {
std::shared_ptr<void> object_ptr;
public:
Object() {}
template<typename T>
Object(T&& object)
: object_ptr {new T {std::move(object)} } {}
virtual ~Object() {};
};
My main cpp file is:
#include <iostream>
#include "Object.hpp"
class Foo {};
int main() {
Object o {Foo{}};
}
It gives me error:
test/test.cpp:13:20: required from here
include/Object.hpp:24:49: error: could not convert ‘{std::move<Foo&>((* & object))}’ from ‘<brace-enclosed initializer list>’ to ‘Foo’
: object_ptr {new T {std::move(object)} } {}
^
include/Object.hpp:24:49: error: no matching function for call to ‘std::shared_ptr<void>::shared_ptr(<brace-enclosed initializer list>)’
include/Object.hpp:24:49: note: candidates are:
In file included from /usr/include/c++/4.8/memory:82:0,
from include/Object.hpp:7,
from test/test.cpp:2:
/usr/include/c++/4.8/bits/shared_ptr.h:314:2: note: template<class _Alloc, class ... _Args> std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...)
shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
^
/usr/include/c++/4.8/bits/shared_ptr.h:314:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:265:17: note: constexpr std::shared_ptr<_Tp>::shared_ptr(std::nullptr_t) [with _Tp = void; std::nullptr_t = std::nullptr_t]
constexpr shared_ptr(nullptr_t __p) noexcept
^
/usr/include/c++/4.8/bits/shared_ptr.h:265:17: note: no known conversion for argument 1 from ‘<type error>’ to ‘std::nullptr_t’
/usr/include/c++/4.8/bits/shared_ptr.h:257:2: note: template<class _Tp1, class _Del> std::shared_ptr<_Tp>::shared_ptr(std::unique_ptr<_Up, _Ep>&&)
shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
^
/usr/include/c++/4.8/bits/shared_ptr.h:257:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:253:2: note: template<class _Tp1> std::shared_ptr<_Tp>::shared_ptr(std::auto_ptr<_Up>&&)
shared_ptr(std::auto_ptr<_Tp1>&& __r);
^
/usr/include/c++/4.8/bits/shared_ptr.h:253:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:248:11: note: template<class _Tp1> std::shared_ptr<_Tp>::shared_ptr(const std::weak_ptr<_Tp1>&)
explicit shared_ptr(const weak_ptr<_Tp1>& __r)
^
/usr/include/c++/4.8/bits/shared_ptr.h:248:11: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:236:2: note: template<class _Tp1, class> std::shared_ptr<_Tp>::shared_ptr(std::shared_ptr<_Tp1>&&)
shared_ptr(shared_ptr<_Tp1>&& __r) noexcept
^
/usr/include/c++/4.8/bits/shared_ptr.h:236:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:226:7: note: std::shared_ptr<_Tp>::shared_ptr(std::shared_ptr<_Tp>&&) [with _Tp = void]
shared_ptr(shared_ptr&& __r) noexcept
^
/usr/include/c++/4.8/bits/shared_ptr.h:226:7: note: no known conversion for argument 1 from ‘<type error>’ to ‘std::shared_ptr<void>&&’
/usr/include/c++/4.8/bits/shared_ptr.h:218:2: note: template<class _Tp1, class> std::shared_ptr<_Tp>::shared_ptr(const std::shared_ptr<_Tp1>&)
shared_ptr(const shared_ptr<_Tp1>& __r) noexcept
^
/usr/include/c++/4.8/bits/shared_ptr.h:218:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:206:2: note: template<class _Tp1> std::shared_ptr<_Tp>::shared_ptr(const std::shared_ptr<_Tp1>&, _Tp*)
shared_ptr(const shared_ptr<_Tp1>& __r, _Tp* __p) noexcept
^
/usr/include/c++/4.8/bits/shared_ptr.h:206:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:184:2: note: template<class _Deleter, class _Alloc> std::shared_ptr<_Tp>::shared_ptr(std::nullptr_t, _Deleter, _Alloc)
shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
^
/usr/include/c++/4.8/bits/shared_ptr.h:184:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:165:2: note: template<class _Tp1, class _Deleter, class _Alloc> std::shared_ptr<_Tp>::shared_ptr(_Tp1*, _Deleter, _Alloc)
shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
^
/usr/include/c++/4.8/bits/shared_ptr.h:165:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:146:2: note: template<class _Deleter> std::shared_ptr<_Tp>::shared_ptr(std::nullptr_t, _Deleter)
shared_ptr(nullptr_t __p, _Deleter __d)
^
/usr/include/c++/4.8/bits/shared_ptr.h:146:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:129:2: note: template<class _Tp1, class _Deleter> std::shared_ptr<_Tp>::shared_ptr(_Tp1*, _Deleter)
shared_ptr(_Tp1* __p, _Deleter __d)
^
/usr/include/c++/4.8/bits/shared_ptr.h:129:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:112:11: note: template<class _Tp1> std::shared_ptr<_Tp>::shared_ptr(_Tp1*)
explicit shared_ptr(_Tp1* __p)
^
/usr/include/c++/4.8/bits/shared_ptr.h:112:11: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:103:7: note: std::shared_ptr<_Tp>::shared_ptr(const std::shared_ptr<_Tp>&) [with _Tp = void]
shared_ptr(const shared_ptr&) noexcept = default;
^
/usr/include/c++/4.8/bits/shared_ptr.h:103:7: note: no known conversion for argument 1 from ‘<type error>’ to ‘const std::shared_ptr<void>&’
/usr/include/c++/4.8/bits/shared_ptr.h:100:17: note: constexpr std::shared_ptr<_Tp>::shared_ptr() [with _Tp = void]
constexpr shared_ptr() noexcept
^
/usr/include/c++/4.8/bits/shared_ptr.h:100:17: note: candidate expects 0 arguments, 1 provided
make: *** [test.o] Error 1
However if I change new T {std::move(object)} to new T (std::move(object)). It works:
class Object {
std::shared_ptr<void> object_ptr;
public:
Object() {}
template<typename T>
Object(T&& object)
: object_ptr {new T (std::move(object)) } {}
virtual ~Object() {};
};
Why curly brace uniform initialization does not work here? Why is considered an initializater list in this case? Foo does not even have a constructor taking initializer list?
This was a known issue, and a defect in the standard (see CWG #1467). The proposal has been applied to the latest working paper (§8.5.4 [dcl.init.list]/3):
List-initialization of an object or reference of type T is defined as follows:
If T is a class type and the initializer list has a single element of type cv U, where U is T or a class derived
from T, the object is initialized from that element (by copy-initialization for copy-list-initialization, or
by direct-initialization for direct-list-initialization).
Note that trunk versions of Clang and GCC accept this code.

gcc 4.7 STL library deficit on pair implementation?

The following code compiles on gcc 4.6 but not 4.7. Is it 4.7's problem or 4.6's problem? Compiled with -std=gnu++0x.
#include <utility>
using namespace std;
struct Z {
};
struct X {
operator Z*() const { return nullptr; }
};
struct Y {
Y(Z*) {}
};
int main() {
pair<int, Y> p(make_pair(0, X()));
}
Error messages:
[hidden]$ g++-mp-4.6 -std=gnu++0x e.cpp
[hidden]$ g++-mp-4.7 -std=gnu++0x e.cpp
e.cpp: In function 'int main()':
e.cpp:17:37: error: no matching function for call to 'std::pair<int, Y>::pair(std::pair<int, X>)'
e.cpp:17:37: note: candidates are:
In file included from /opt/local/include/gcc47/c++/utility:72:0,
from e.cpp:1:
/opt/local/include/gcc47/c++/bits/stl_pair.h:204:9: note: template<class ... _Args1, long unsigned int ..._Indexes1, class ... _Args2, long unsigned int ..._Indexes2> std::pair::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>)
/opt/local/include/gcc47/c++/bits/stl_pair.h:204:9: note: template argument deduction/substitution failed:
e.cpp:17:37: note: 'std::pair<int, X>' is not derived from 'std::tuple<_Args1 ...>'
In file included from /opt/local/include/gcc47/c++/utility:72:0,
from e.cpp:1:
/opt/local/include/gcc47/c++/bits/stl_pair.h:153:9: note: template<class ... _Args1, class ... _Args2> std::pair::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>)
/opt/local/include/gcc47/c++/bits/stl_pair.h:153:9: note: template argument deduction/substitution failed:
e.cpp:17:37: note: cannot convert 'std::make_pair(_T1&&, _T2&&) [with _T1 = int; _T2 = X; typename std::__decay_and_strip<_T2>::__type = X; typename std::__decay_and_strip<_T1>::__type = int]((* & X()))' (type 'std::pair<int, X>') to type 'std::piecewise_construct_t'
In file included from /opt/local/include/gcc47/c++/utility:72:0,
from e.cpp:1:
/opt/local/include/gcc47/c++/bits/stl_pair.h:148:12: note: template<class _U1, class _U2, class> constexpr std::pair::pair(std::pair<_U1, _U2>&&)
/opt/local/include/gcc47/c++/bits/stl_pair.h:148:12: note: template argument deduction/substitution failed:
/opt/local/include/gcc47/c++/bits/stl_pair.h:145:38: error: no type named 'type' in 'struct std::enable_if<false, void>'
/opt/local/include/gcc47/c++/bits/stl_pair.h:142:12: note: template<class _U1, class _U2, class> constexpr std::pair::pair(_U1&&, _U2&&)
/opt/local/include/gcc47/c++/bits/stl_pair.h:142:12: note: template argument deduction/substitution failed:
e.cpp:17:37: note: candidate expects 2 arguments, 1 provided
In file included from /opt/local/include/gcc47/c++/utility:72:0,
from e.cpp:1:
/opt/local/include/gcc47/c++/bits/stl_pair.h:136:12: note: template<class _U2, class> constexpr std::pair::pair(const _T1&, _U2&&)
/opt/local/include/gcc47/c++/bits/stl_pair.h:136:12: note: template argument deduction/substitution failed:
e.cpp:17:37: note: cannot convert 'std::make_pair(_T1&&, _T2&&) [with _T1 = int; _T2 = X; typename std::__decay_and_strip<_T2>::__type = X; typename std::__decay_and_strip<_T1>::__type = int]((* & X()))' (type 'std::pair<int, X>') to type 'const int&'
In file included from /opt/local/include/gcc47/c++/utility:72:0,
from e.cpp:1:
/opt/local/include/gcc47/c++/bits/stl_pair.h:131:12: note: template<class _U1, class> constexpr std::pair::pair(_U1&&, const _T2&)
/opt/local/include/gcc47/c++/bits/stl_pair.h:131:12: note: template argument deduction/substitution failed:
e.cpp:17:37: note: candidate expects 2 arguments, 1 provided
In file included from /opt/local/include/gcc47/c++/utility:72:0,
from e.cpp:1:
/opt/local/include/gcc47/c++/bits/stl_pair.h:122:7: note: std::pair<_T1, _T2>::pair(std::pair<_T1, _T2>&&) [with _T1 = int; _T2 = Y; std::pair<_T1, _T2> = std::pair<int, Y>]
/opt/local/include/gcc47/c++/bits/stl_pair.h:122:7: note: no known conversion for argument 1 from 'std::pair<int, X>' to 'std::pair<int, Y>&&'
/opt/local/include/gcc47/c++/bits/stl_pair.h:119:17: note: constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = int; _T2 = Y; std::pair<_T1, _T2> = std::pair<int, Y>]
/opt/local/include/gcc47/c++/bits/stl_pair.h:119:17: note: no known conversion for argument 1 from 'std::pair<int, X>' to 'const std::pair<int, Y>&'
/opt/local/include/gcc47/c++/bits/stl_pair.h:116:12: note: template<class _U1, class _U2, class> constexpr std::pair::pair(const std::pair<_U1, _U2>&)
/opt/local/include/gcc47/c++/bits/stl_pair.h:116:12: note: template argument deduction/substitution failed:
/opt/local/include/gcc47/c++/bits/stl_pair.h:113:38: error: no type named 'type' in 'struct std::enable_if<false, void>'
/opt/local/include/gcc47/c++/bits/stl_pair.h:104:26: note: constexpr std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = int; _T2 = Y]
/opt/local/include/gcc47/c++/bits/stl_pair.h:104:26: note: candidate expects 2 arguments, 1 provided
/opt/local/include/gcc47/c++/bits/stl_pair.h:100:26: note: constexpr std::pair<_T1, _T2>::pair() [with _T1 = int; _T2 = Y]
/opt/local/include/gcc47/c++/bits/stl_pair.h:100:26: note: candidate expects 0 arguments, 1 provided
That shouldn't compile.
The initialisation of p.second requires an implicit conversion from X to Y. An implicit conversion can only involve at most one user-defined conversion The required conversion would require two; X to Z* via the conversion operator, and Z* to Y via the conversion constructor.
Initialisation of pair elements from another pair is only allowed via implicit conversions. C++11 says:
20.3.2/12 This constructor shall not participate in overload resolution unless const U& is implicitly convertible to first_type and const V& is implicitly convertible to second_type.
and C++98 said:
20.2.2/4 Initializes members from the corresponding members of the argument, performing implicit conversions as needed.
Presumably, the older version had a bug which allowed this conversion to be considered, and that bug has been fixed in the more recent version.