У меня возникают проблемы с выяснением того, как предотвратить рекурсивные попытки замены шаблона и явно принудительно создать экземпляр шаблона подкласса
Следующий код:
#include <boost/ptr_container/ptr_vector.hpp>
template <typename TemplateParameter>
struct ParentClass
{
struct SubClass
{
boost::ptr_vector<SubClass> nodes;
}; // this by itself works fine
// but with combination with this
void someMethod(boost::ptr_vector<SubClass>& otherNodes){}
};
int main (int argc, char** argv)
{
ParentClass<int> p;
return 0;
}
, созданный с бустом 1.68 иgcc 7.3.1 выдает следующую ошибку:
test.cpp: In instantiation of ‘struct ParentClass<int>::SubClass’:
/usr/include/boost/ptr_container/nullable.hpp:55:13: recursively required by substitution of ‘template<class T> boost::type_traits::yes_type boost::ptr_container_detail::is_nullable(const boost::nullable<T>*) [with T = <missing>]’
/usr/include/boost/ptr_container/nullable.hpp:55:13: required from ‘const bool boost::is_nullable<ParentClass<int>::SubClass>::value’
/usr/include/boost/mpl/if.hpp:63:11: required from ‘struct boost::mpl::if_<boost::is_nullable<ParentClass<int>::SubClass>, ParentClass<int>::SubClass, boost::mpl::identity<ParentClass<int>::SubClass> >’
/usr/include/boost/mpl/eval_if.hpp:37:41: required from ‘struct boost::mpl::eval_if<boost::is_nullable<ParentClass<int>::SubClass>, ParentClass<int>::SubClass, boost::mpl::identity<ParentClass<int>::SubClass> >’
/usr/include/boost/ptr_container/nullable.hpp:69:13: required from ‘struct boost::remove_nullable<ParentClass<int>::SubClass>’
/usr/include/boost/ptr_container/nullable.hpp:80:55: required from ‘struct boost::ptr_container_detail::void_ptr<ParentClass<int>::SubClass>’
test.cpp:11:10: required from ‘struct ParentClass<int>’
test.cpp:16:22: required from here
test.cpp:8:37: error: invalid use of incomplete type ‘struct boost::ptr_container_detail::void_ptr<ParentClass<int>::SubClass>’
boost::ptr_vector<SubClass> nodes;
^~~~~
In file included from /usr/include/boost/ptr_container/detail/reversible_ptr_container.hpp:25,
from /usr/include/boost/ptr_container/ptr_sequence_adapter.hpp:20,
from /usr/include/boost/ptr_container/ptr_vector.hpp:20,
from test.cpp:1:
/usr/include/boost/ptr_container/nullable.hpp:75:16: note: declaration of ‘struct boost::ptr_container_detail::void_ptr<ParentClass<int>::SubClass>’
struct void_ptr
^~~~~~~~
Насколько я понимаю, проблема заключается в том, что он пытается разрешить ParentClass
и, следовательно, пытается разрешить свой метод - someMehtod(...)
, но для этогодолжен разрешить SubClass
, который определен.Обходной путь - заставить явное создание экземпляра путем создания экземпляра SubClass
как члена ParentClass
:
struct SubClass
{
boost::ptr_vector<SubClass> nodes;
} member;
Есть ли другой, менее хакерский способ?:)
Проблема отсутствовала в более старой версии boost, где boost::ptr_vector
был реализован с void*
вместо void_ptr
шаблонного класса.