- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我为二叉树编写了一个基本程序如下
#include <iostream>
#include <memory>
template<typename T>
using sp = std::unique_ptr<T>;
template<typename T>
struct Node{
Node(T val):
x(val){
}
const sp<Node>& addL(T val){
left = std::make_unique<Node>(val);
return left;
}
const sp<Node>& addR(T val){
right = std::make_unique<Node>(val);
return right;
}
private:
T x;
sp<Node> left;
sp<Node> right;
};
int main(){
auto root = std::make_unique<Node<int>>(5);
root->addL(10)->addR(4)->addL(12);
root->addR(14)->addL(3)->addR(15);
}
auto root = std::make_unique<Node<int>>(5);
<int>
模板参数然后编译器提示模板推导失败
tree.cpp:44:41: error: no matching function for call to ‘make_unique<template<class T> struct Node>(int)’
44 | auto root = std::make_unique<Node>(5);
| ^
In file included from /usr/include/c++/9/memory:80,
from tree.cpp:2:
/usr/include/c++/9/bits/unique_ptr.h:848:5: note: candidate: ‘template<class _Tp, class ... _Args> typename std::_MakeUniq<_Tp>::__single_object std::make_unique(_Args&& ...)’
848 | make_unique(_Args&&... __args)
| ^~~~~~~~~~~
/usr/include/c++/9/bits/unique_ptr.h:848:5: note: template argument deduction/substitution failed:
left = std::make_unique<Node>(val);
<int>
不需要吗?这是否也解释了为什么
sp<Node<T>>
类函数和
sp<Node>
的签名中不需要足以让编译器推断实际类型。
g++ (Ubuntu 9.2.1-17ubuntu1~16.04) 9.2.1 20191102
最佳答案
与 injected-class-name ,
在类 Node<T>
的范围内, Node
也指Node<T>
.
所以,在
left = std::make_unique<Node>(val); // Inside class scope
left = std::make_unique<Node<T>>(val); // Inside class scope
Node
引用模板类。
auto root = std::make_unique<Node>(5); // Invalid
make_unique
的模板参数是类型而不是模板模板参数。
auto root = std::make_unique<Node<int>>(5);
关于c++ - 模板推演句法差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60201126/
在对 Comparable 和 Comparator 进行一些研究后,我遇到了一些看起来很有用的有趣语法(但我不明白)。 示例: public static Comparator TypeCompar
我现在已经在一些库中看到了这种语法,我想知道它有什么好处。 (注意我很清楚闭包和代码在做什么,我只关心语法差异) !function(){ // do stuff }(); 作为更常见的替代品 (
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
这个问题已经有答案了: Why isn't my JavaScript working in JSFiddle? (7 个回答) Uncaught reference error: Function
我现在在一些库上看到了这种语法,我想知道它的好处是什么。 (请注意,我很清楚闭包以及代码在做什么,我只关心语法差异) !function(){ // do stuff }(); 作为更常见的替代方
如何让下面的代码工作?我找不到适合我的语法。 #Create the table if table_exists?(tablename) puts "table found" else ActiveR
我需要做这样的事情: $('#online-order').wcForms({id: '#online-order', to: 'contact', colors['red']: '#00F' });
我有三个哈希: a = {key:"value"} b = {"key"=>"value"} c = {:key=>"value"} 这些结果: a.has_key?("key") # => fals
language suggestion指出在链接的文件中详细说明了这些优点。我快速浏览了一下,但看不到它的明确说明。 优点是每个语句都并行执行,所以我可能会得到速度提升? 或者是否有某种逻辑它迎合,使
我正在 here 读取一个 js 文件,在此 js 文件的最顶部,您可以找到以下几行: var gsAgent=navigator.userAgent.toLowerCase(), gsAppVer=
很抱歉问这样一个新手问题,但我一直在尝试理解这个 SQL 查询并添加一个东西,但我无法做到这一点。 这里是查询: select p.id, p.nick, p.creation_date fr
我创建了一个表“table2”并在运行我的代码时收到警告(表已存在)。我只想在表不存在时创建表。一些研究MySQL syntax website在 MySQL 中出现以下内容:如果不存在则创建表 我的
我是一名优秀的程序员,十分优秀!