简体   繁体   中英

C++ template class call own constructor from static helper

I've run into a pickle with a template class:

template <
          class N
         >
class Edge
{
  public:

    typedef std::shared_ptr<N> N_ptr;

    Edge
    (
      N_ptr node
     )
    {
       //...
    }

    template <class Archive>
    static Edge<N> * load_and_allocate( Archive & ar )
    {
      N_ptr node;
      ar( node );
      return new ::template Edge<N>( node );
    }
};

The method load_and_allocate is required by cereal for deserialization of objects without a default empty constructor.

For the return line for load_and_allocate, the compiler throws:

/usr/include/c++/4.7/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = Edge<Concept>; _Args = {}]’:
/usr/include/c++/4.7/bits/stl_uninitialized.h:497:3:   required from ‘static void std::__uninitialized_default_n_1<_TrivialValueType>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Edge<Concept>*; _Size = long unsigned int; bool _TrivialValueType = false]’
/usr/include/c++/4.7/bits/stl_uninitialized.h:545:7:   required from ‘void std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Edge<Concept>*; _Size = long unsigned int]’
/usr/include/c++/4.7/bits/stl_uninitialized.h:607:7:   required from ‘void std::__uninitialized_default_n_a(_ForwardIterator, _Size, std::allocator<_Tp>&) [with _ForwardIterator = Edge<Concept>*; _Size = long unsigned int; _Tp = Edge<Concept>]’
/usr/include/c++/4.7/bits/vector.tcc:541:8:   required from ‘void std::vector<_Tp, _Alloc>::_M_default_append(std::vector<_Tp, _Alloc>::size_type) [with _Tp = Edge<Concept>; _Alloc = std::allocator<Edge<Concept> >; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/4.7/bits/stl_vector.h:647:4:   required from ‘void std::vector<_Tp, _Alloc>::resize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = Edge<Concept>; _Alloc = std::allocator<Edge<Concept> >; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
/usr/local/include/cereal/types/vector.hpp:83:5:   [ skipping 31 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/Daemon/../ConnectionHandler/../../Processes/Controller/../../Datatypes/Domain/../../Handlers/SemanticNodeFactory/SemanticNodeFactory.hpp:34:82:   required from here
/usr/include/c++/4.7/bits/stl_construct.h:77:7: error: no matching function for call to ‘Edge<Concept>::Edge()’
/usr/include/c++/4.7/bits/stl_construct.h:77:7: note: candidates are:
candidate expects 1 argument, 0 provided
Edge<Relation>::Edge(const Edge<Relation>&)

Message repeats for other template (Concept & Relation).

Same thing happens for:

return new ::template Edge<N>( node );

and

return new Edge<N>::template Edge( node );

or

return new Edge<N>::template Edge<N>( node );

or

return new Edge<N>( node );

In fact, most complain about the qualifier, with only the first return statement not creating any errors about the qualifier.

Could someone please explain why is it not picking up the only available constructor, and ignores the parameter I pass to it?

Weirdly enough, if I declare an empty default public constructor:

Edge( ){ }

It compiles without an error.

Resizing a vector of T requires T to be default constructible or you to provide a default value as a second argument to 'resize'.

In your case Edge is not default constructible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM