【问题标题】:C++ template class call own constructor from static helperC++模板类从静态助手调用自己的构造函数
【发布时间】:2013-11-07 20:56:08
【问题描述】:

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

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 语句没有产生任何关于限定符的错误。

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

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

Edge( ){ }

它编译没有错误。

【问题讨论】:

  • 我认为只需调用new Edge(node) 即可满足您的需求。
  • @Arkadiy 不,恐怕会产生完全相同的错误
  • 该代码适用于我的minimal compiling example。该问题可能与您的文件结构(头文件)有关。您可以在类中使用 injected-class-name Edge 而不使用模板参数。
  • Archive 的类型如何,你如何称呼load_and_allocate(Archive &amp; ar )
  • @Drop 我不调用 load_and_allocate,它在反序列化时由 lib grain 调用,而不是调用默认构造函数。

标签: c++ templates static-methods


【解决方案1】:

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

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

【讨论】:

  • 当然,我在错误消息中也看到了这一点 - 但是尝试制作 Edge 向量的代码在哪里?
  • 它看起来像是在“谷物”库中的某个地方。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-12
  • 2012-12-10
  • 2013-01-11
  • 1970-01-01
  • 2011-10-07
  • 2011-09-15
相关资源
最近更新 更多