函子"can't deduce template parameter"的模板方法
template method of functor "can't deduce template parameter"
g++与std=c++14在函数类的模板方法(本身不是模板)上给我一个"无法推导模板参数'Key'"错误。我不明白为什么。代码看起来应该可以工作。
我正在实现一个23树,它有一个接受函子的级别顺序遍历方法。操作人员tree23代码基本上是这样的:
template<class Key, class Value> class tree23 {
public:
class Node23 {
friend class tree23<Key, Value>;
public:
// snip...
friend std::ostream& operator<<(std::ostream& ostr, const Node23& node23)
{
// snip... outputs the keys and values of node23 to ostr.
}
private:
Node23 *parent;
std::array<Key, 2> keys;
std::array<Value, 2> values;
std::array<std::unique_ptr<Node23>, 3> children;
int totalItems; // either TwoNodeItems or ThreeNodeItems
// snip...
};
template<typename Functor> void levelOrderTraverse(Functor f) const noexcept;
// snip...
};
级别顺序遍历调用函子的函数调用运算符,并向其传递两个参数。
template<class Key, class Value> template<typename Functor>
void tree23<Key, Value>::levelOrderTraverse(Functor f) const noexcept
{
std::queue< std::pair<const Node23*, int> > queue;
Node23 *proot = root.get();
if (proot == nullptr) return;
auto initial_level = 1; // initial, top level is 1, the root.
queue.push(std::make_pair(proot, initial_level));
while (!queue.empty()) {
std::pair<const Node23 *, int> pair_ = queue.front();
const Node23 *current = pair_.first;
int current_tree_level = pair_.second;
// invokes functor's operator()(const Node23&, int)?
f(*current, current_tree_level);
if (!current->isLeaf()) {
for(auto i = 0; i < current->getChildCount(); ++i) {
queue.push(std::make_pair(current->children[i].get(), current_tree_level + 1));
}
}
queue.pop();
}
}
函子很简单:
class levelOrderPrinter {
private:
// snip...
public:
levelOrderPrinter(std::ostream& ostr_lhs, int depth);
levelOrderPrinter(levelOrderPrinter&);
levelOrderPrinter(const levelOrderPrinter&);
template<class Key, class Value>
void operator()(const typename tree23<Key, Value>::Node23& node,
int current_level) noexcept;
};
template<class Key, class Value>
void levelOrderPrinter::operator()(const typename tree23<Key, Value>::Node23& node,
int current_level) noexcept
{
// Did level change?
if (level != current_level) {
level = current_level;
ostr << "nn" << "level = " << level;
// Provide some basic spacing to tree appearance.
std::size_t num = tree_depth - level + 1;
std::string str( num, ' ');
ostr << str;
}
ostr << node;
}
如果您更改函数调用运算符的声明,如下所示:
template<class Node>
void levelOrderPrinter::operator()(const Node& node, int current_level) noexcept
然后编译器将能够推导出Node
的类型。
在您的原始代码中:
template<class Key, class Value>
void levelOrderPrinter::operator()(const typename tree23<Key, Value>::Node23& node,
int current_level) noexcept
编译器无法推导类型Key
或Value
,因为tree23<Key, Value>::Node23
是Key
和Value
的非推导上下文(§14.8.2.5),迫使您使用显式函数模板调用语法。
更改此行
f(*current, current_tree_level);
在方法中
template<class Key, class Value> template<typename Functor>
void tree23<Key, Value>::levelOrderTraverse(Functor f) const noexcept
成为
f.template operator()<Key, Value>(*current, current_tree_level);
摆脱了"无法推导模板参数"的错误。它不漂亮,但现在可以编译了。
Node23是一个嵌套类型,其封闭类型的模板参数无法轻易推导。因此,您必须通过template operator()
明确指定它们。
相关文章:
- 表示"accepting anything for this template argument" C++概念的通配符
- 传递给std::function template的template参数究竟代表什么
- 在template中使用std::variant的template函数
- C++模板错误:"invalid explicitly-specified argument for template parameter"
- 为什么这段代码抱怨"a non-type template parameter cannot have type"?
- 如何抛出"template parameter"类型的异常?
- 重新定义类<template-parameter-1-2>的默认参数
- 为什么我会收到错误"parameter pack 'F' must be at the end of the template parameter list"
- C++:在模板快速排序功能中接收"couldn't deduce template parameter"错误
- MEX 编译给出"there are no arguments to ... that depend on a template parameter"错误
- Clang VS VC++: "error: declaration of 'T' shadows template parameter"
- 函子"can't deduce template parameter"的模板方法
- 对于相互转换的类型,有没有办法绕过"deduced conflicting types for parameter in template instantiation"?
- 模板专用化站点报告"too few template-parameter-lists"错误
- 模板专业化 -> 'too few template-parameter-lists'
- "Name The Template Parameter" 奇数定义
- 具有Non-type template Parameter成员函数的template类
- 等同于"typename",表示从属姓名确实是'template template parameter'
- 模板参数的template-parameter-list是什么意思?
- "multiple template parameter lists are not allowed" ?