C++ 模板类从静态助手调用自己的构造函数

标签 c++ templates static-methods

我遇到了一个带有模板类的泡菜:

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 );
    }
};

cereal 需要方法 load_and_allocate用于反序列化没有默认空构造函数的对象。

对于 load_and_allocate 的返回行,编译器抛出:

/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>&)

消息重复其他模板(概念和关系)。

同样的事情发生在:

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

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

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

return new Edge<N>( node );

事实上,大多数人提示限定符,只有第一个 return 语句没有产生任何关于限定符的错误。

有人能解释一下为什么它不选择唯一可用的构造函数,而忽略我传递给它的参数吗?

奇怪的是,如果我声明一个空的默认公共(public)构造函数:

Edge( ){ }

它编译没有错误。

最佳答案

调整 T vector 的大小要求 T 是默认可构造的,或者您提供默认值作为“调整大小”的第二个参数。

在您的情况下,Edge 不是默认可构造的。

关于C++ 模板类从静态助手调用自己的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19846533/

相关文章:

c++ - 使用Linux pread可以避免 “unavailability of data for reading written by a different thread”吗?

c++ - 我可以假设兄弟类的静态转换将无法编译吗?

c++ - 函数模板和类模板有什么区别?

c# - 静态方法中的 HttpContext.Current.Response

c++ - 为什么数组不打印第一个元素?

c++ - 使用自定义序列点绘制轮廓?

java - 在 Android studio 中使用 Activity 模板时无法添加新的 Java 类吗?

python - 在渲染之前对 django 模板节点进行后处理以组合标签

java - 为什么我不能以这种方式在静态方法中返回任何内容

php - 访问静态变量定义中的静态方法