我正在尝试实现模板二叉树类(用于学习)。
我有以下定义:
template <typename T>
class binary_tree final
{
...
public:
template <typename TParam,
typename = enable_if_t<std::is_constructible<T, remove_reference_t<TParam>>::value>>
void insert(TParam &&data);
};
我正在尝试在头文件底部添加insert
的实现:
template<typename T>
template <typename TParam,
typename = enable_if_t<std::is_constructible<T, remove_reference_t<TParam>>::value>>
void binary_tree<T>::insert(TParam &&data)
{
binary_tree_ptr &insert_location = data > _data ? _right : _left;
if (!insert_location)
insert_location = binary_tree_ptr(new binary_tree(std::forward<TParam>(data)));
else
insert_location->insert(std::forward<TParam>(data));
}
这将无法编译,并出现以下错误,指向实现:
cannot add a default template argument to the definition of a member of a class template
如果我从实现中删除typename = ...
,则会出现以下错误:
out-of-line definition of 'insert' does not match any declaration in 'binary_tree<T>'
从声明中删除typename = ...
不会带来更好的结果。
我想念什么?有办法吗?
我正在使用clang 6进行编译。