- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在下面的代码中,为什么我在 n = 8
时出现编译错误,而所有其他情况都正常?我的目的是报告一些错误并返回空指针,而不用不必要的大括号使代码困惑。我将使用 nullptr 来实现此目的,但我很好奇为什么 {}
无法使用逗号运算符进行编译,而它单独工作。
您可以使用此代码 here 。我使用了 C++20 设置。
#include <cstdint>
#include <memory>
void oops(const char*) {
}
std::unique_ptr<uint8_t[]> please_do(int n) {
if (n == 1)
return std::unique_ptr<uint8_t[]>(); // compiles ok
if (n == 2)
return oops(""), std::unique_ptr<uint8_t[]>(); // compiles ok
if (n == 3)
return std::make_unique<uint8_t[]>(n); // compiles ok
if (n == 4)
return oops(""), std::make_unique<uint8_t[]>(n); // compiles ok
if (n == 5)
return nullptr; // compiles ok
if (n == 6)
return oops(""), nullptr; // compiles ok
if (n == 7)
return {}; // compiles ok
if (n == 8)
return oops(""), {}; // why compilation error?
return nullptr; // compiles ok
}
int main() {
please_do(42);
return 0;
}
GCC 9.2 输出:
<source>: In function 'std::unique_ptr<unsigned char []> please_do(int)':
<source>:26:26: error: expected primary-expression before '{' token
26 | return oops(""), {}; // why compilation error?
| ^
<source>:26:25: error: expected ';' before '{' token
26 | return oops(""), {}; // why compilation error?
| ^~
| ;
Clang 9.0.0 输出:
<source>:26:16: error: no viable conversion from returned value of type 'void' to function return type 'std::unique_ptr<uint8_t []>'
return oops(""), {}; // why compilation error?
^~~~~~~~
/opt/compiler-explorer/gcc-9.2.0/lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0/bits/unique_ptr.h:528:7: note: candidate constructor not viable: cannot convert argument of incomplete type 'void' to 'std::unique_ptr<unsigned char [], std::default_delete<unsigned char []> > &&' for 1st argument
unique_ptr(unique_ptr&& __u) noexcept
^
/opt/compiler-explorer/gcc-9.2.0/lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0/bits/unique_ptr.h:533:12: note: candidate constructor template not viable: cannot convert argument of incomplete type 'void' to 'std::nullptr_t' (aka 'nullptr_t') for 1st argument
constexpr unique_ptr(nullptr_t) noexcept
^
/opt/compiler-explorer/gcc-9.2.0/lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0/bits/unique_ptr.h:681:7: note: candidate constructor not viable: cannot convert argument of incomplete type 'void' to 'const std::unique_ptr<unsigned char [], std::default_delete<unsigned char []> > &' for 1st argument
unique_ptr(const unique_ptr&) = delete;
^
/opt/compiler-explorer/gcc-9.2.0/lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0/bits/unique_ptr.h:542:2: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'void'
unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
^
<source>:26:24: error: expected expression
return oops(""), {}; // why compilation error?
^
最佳答案
{}
不是表达式。 return {...};
是它自己的特殊语法( list initialization 的形式),它从函数的签名中计算出返回类型,就像通过 Ret{...}
其中 Ret
是返回类型:
Ret f() {
return {...}; // special syntax for return; {args...} need not be a valid expression
}
// same as
Ret f() {
return Ret{...}; // generic return <expression> syntax; Ret{args...} must be a valid expression
}
return a, b
中没有特殊语法。它只是使用正常 return
语法返回表达式 a, b
。逗号运算符要求 a
和 b
都是表达式,但由于 {}
不是表达式,因此表达式与 oops("") 类似,{}
是无效语法。
顺便说一句,还有其他地方可以使用 {...}
,使其看起来像是一个表达式,但实际上不是,例如函数调用:
void f(std::string);
f({5, 'a'})
同样,虽然这看起来像 {5, 'a'}
是一个类型为 std::string
的表达式(正如我们的意图),但事实并非如此。 {5, 'a'}
是函数调用本身的一部分,其类型由重载解析决定。
至于编译器错误,它们都因 oops(""), {}
作为表达式的语法无效而感到困惑。 GCC 似乎已读取 oops(""),
,因此期望后面有一个表达式。但是表达式不能以 {
开头,因此它读取该字符并立即呕吐,提示它期望表达式开始。 Clang 也做同样的事情。我认为 Clang 然后会继续追求“有帮助”,假装您只是没有编写 , {};
部分(也就是说,就好像您编写了 return oops("");
),并为此发出错误。这种行为例如是什么让 Clang 能够提供有用的类型错误,即使你拼写错误;也就是说,如果您要运行 Clang
void supercalifragilisticexpialidocious(std::string);
int main() { supercalifragilisticexpialidociuos(7); }
它首先会因拼写错误 (-ous -> -uos
) 发出错误,建议正确的拼写,然后会因 之间的类型不匹配发出错误int
7
和参数类型 std::string
就好像您已经修复了拼写一样。同样,在这里,它会因错误语法发出一个错误,然后发出第二个错误,就好像您已经摆脱了错误语法一样。但是,在这种情况下它并不是很有帮助,因为它想象您使用的“修复”并不是实际的修复。
关于c++ - 当 return 语句、逗号运算符、花括号初始化列表和 std::unique_ptr 组合在一起时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59473651/
我是 Spring 新手,这就是我想要做的事情: 我正在使用一个基于 Maven 的库,它有自己的 Spring 上下文和 Autowiring 字段。 它的bean配置文件是src/test/res
我在我的测试脚本中有以下列表初始化: newSequenceCore=["ls", "ns", "*", "cm", "*", "ov", "ov", "ov", "ov", "kd"] (代表要在控
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Class construction with initial values 当我查看 http://en.
我得到了成员变量“objectCount”的限定错误。编译器还返回“ISO C++ 禁止非常量静态成员的类内初始化”。这是主类: #include #include "Tree.h" using n
我有如下所示的a.h class A { public: void doSomething()=0; }; 然后我有如下所示的b.h #include "a.h" class b: publi
我需要解析 Firebase DataSnapshot (一个 JSON 对象)转换成一个数据类,其属性包括 enum 和 list。所以我更喜欢通过传递 DataSnapshot 来手动解析它进入二
我使用 JQuery 一段时间了,我总是使用以下代码来初始化我的 javascript: $(document).ready( function() { // Initalisation logic
这里是 Objective-C 菜鸟。 为什么会这样: NSString *myString = [NSString alloc]; [myString initWithFormat:@"%f", s
我无法让核心数据支持的 NSArrayController 在我的代码中正常工作。下面是我的代码: pageArrayController = [[NSArrayController alloc] i
我对这一切都很陌生,并且无法将其安装到我的后端代码中。它去哪里?在我的页脚下面有我所有的 JS? 比如,这是什么意思: Popup initialization code should be exec
这可能是一个简单的问题,但是嘿,我是初学者。 所以我创建了一个程序来计算一些东西,它目前正在控制台中运行。我决定向其中添加一个用户界面,因此我使用 NetBeans IDE 中的内置功能创建了一个 J
我有 2 个 Controller ,TEST1Controller 和 TEST2Controller 在TEST2Controller中,我有一个initialize()函数设置属性值。 如果我尝
据我所知, dependentObservable 在声明时会进行计算。但如果某些值尚不存在怎么办? 例如: var viewModel ={}; var dependentObservable1 =
我正在阅读 POODR 这本书,它使用旧语法进行默认值初始化。我想用新语法实现相同的功能。 class Gear attr_reader :chainring, :cog, :wheel de
我按照 polymer 教程的说明进行操作: https://www.polymer-project.org/3.0/start/install-3-0 (我跳过了可选部分) 但是,在我执行命令“po
很抱歉问到一个非常新手的Kotlin问题,但是我正在努力理解与构造函数和初始化有关的一些东西。 我有这个类和构造函数: class TestCaseBuilder constructor(
假设我们有一个包含 30 列和 30 行的网格。 生命游戏规则简而言之: 一个小区有八个相邻小区 当一个细胞拥有三个存活的相邻细胞时,该细胞就会存活 如果一个细胞恰好有两个或三个活的相邻细胞,那么它就
我是 MQTT 和 Android 开放附件“AOA” 的新手。在阅读教程时,我意识到,在尝试写入 ByteArrayOutputStream 类型的变量之前,应该写入 0 或 0x00首先到该变量。
我有 2 个 Controller ,TEST1Controller 和 TEST2Controller 在TEST2Controller中,我有一个initialize()函数设置属性值。 如果我尝
我有一个inotify /内核问题。我正在使用“inotify” Python项目进行观察,但是,我的问题仍然是固有的关于inotify内核实现的核心。 Python inotify项目处理递归ino
我是一名优秀的程序员,十分优秀!