is there a reason why std::make_shared would require a default constructor?
我试图找出这是否是谷物的要求。
我一直在收到错误,因为类构造器(默认构造器)是私有的,我将其放置在这里是有原因的。
但是,错误的始发行似乎是std :: make_shared,而不是谷物,后者需要默认构造函数,但已经是一个朋友类,因此应该可以访问它。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /usr/include/c++/4.7/type_traits: In instantiation of ‘struct std::__is_default_constructible_impl<Concept>’: /usr/include/c++/4.7/type_traits:116:12: required from ‘struct std::__and_<std::__not_<std::is_void<Concept> >, std::__is_default_constructible_impl<Concept> >’ /usr/include/c++/4.7/type_traits:682:12: required from ‘struct std::__is_default_constructible_atom<Concept>’ /usr/include/c++/4.7/type_traits:703:12: required from ‘struct std::__is_default_constructible_safe<Concept, false>’ /usr/include/c++/4.7/type_traits:709:12: required from ‘struct std::is_default_constructible<Concept>’ /usr/local/include/cereal/types/polymorphic.hpp:157:5: required by substitution of ‘template<class Archive, class T> typename std::enable_if<((! std::is_default_constructible< T >::value) && (! has_load_and_allocate<T, Archive>())), bool>::type cereal::polymorphic_detail::serialize_wrapper(Archive&, std::shared_ptr<_Tp2>&, uint32_t) [with Archive = cereal::XMLInputArchive; T = Concept]’ /usr/local/include/cereal/types/polymorphic.hpp:253:5: [ skipping 16 instantiation contexts ] /usr/include/c++/4.7/bits/shared_ptr_base.h:525:8: required from ‘std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, _Tp*, const _Alloc&, _Args&& ...) [with _Tp = SemanticGraph<Concept>; _Alloc = std::allocator<SemanticGraph<Concept> >; _Args = {const char (&)[16]}; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]’ /usr/include/c++/4.7/bits/shared_ptr_base.h:997:35: required from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<SemanticGraph<Concept> >; _Args = {const char (&)[16]}; _Tp = SemanticGraph<Concept>; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]’ /usr/include/c++/4.7/bits/shared_ptr.h:317:64: required from ‘std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<SemanticGraph<Concept> >; _Args = {const char (&)[16]}; _Tp = SemanticGraph<Concept>]’ /usr/include/c++/4.7/bits/shared_ptr.h:599:39: required from ‘std::shared_ptr<_Tp1> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = SemanticGraph<Concept>; _Alloc = std::allocator<SemanticGraph<Concept> >; _Args = {const char (&)[16]}]’ /usr/include/c++/4.7/bits/shared_ptr.h:615:42: required from ‘std::shared_ptr<_Tp1> std::make_shared(_Args&& ...) [with _Tp = SemanticGraph<Concept>; _Args = {const char (&)[16]}]’ /home/alex/projects/Icarus/trunk/Api/ConnectionHandler/../../Processes/Controller/../../Datatypes/Domain/../../Handlers/SemanticNodeFactory/SemanticNodeFactory.hpp:34:82: required from here /home/alex/projects/Icarus/trunk/Api/ConnectionHandler/../../Processes/Controller/../../Datatypes/Domain/../Episode/../State/../ConceptGraph/../Concept/Concept.hpp:27:5: error: ‘Concept::Concept()’ is private |
有人可以向我解释为什么会发生这种情况,更重要的是,除了将这些构造函数公开之外,如何解决它?
编辑:
原始错误行来自:
1 | concept_map = std::make_shared<SemanticGraph<Concept>>("concept_map.xml" ); |
SemanticGraph Ctor在哪里:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | SemanticGraph ( const std::string filename ) : _fname ( filename ) { std::ifstream input( _fname ); if ( input.is_open() ) { cereal::XMLInputArchive archive( input ); archive( _nodes ); } } |
C ++过去在实例化模板时不考虑访问控制,除非在需要时会产生错误。 您使用的编译器仍然使用这些规则。 因此,您的课程不被视为不可默认构造的。 相反,检查本身是不可能的。
GCC 4.8及更高版本确实支持这一点。 一个简单的演示程序,以4.8成功,而以4.7失败:
1 2 3 4 5 6 7 | #include <type_traits> class S { S() {} }; int main() { return std::is_default_constructible::value; } |
在4.8中,它返回0。在4.7中,这产生编译时错误。
要解决此问题,请确保您没有默认的构造函数,甚至没有私有的构造函数。 您可以向构造函数添加一个伪参数,并确保始终传递该伪参数。