- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想从函数返回一个数组(或对数组的引用),如下所示:
decltype(auto) bar() { static int a[2]; return a; }
不幸的是,它会导致非常神秘的错误。海湾合作委员会提示:
error: cannot convert 'int [2]' to 'int [2]' in return
而 Clang 在解释这个问题时也不是更好:
error: array initializer must be an initializer list
演示:
https://gcc.godbolt.org/z/ao7Txa9oP
decltype(auto)
。)
void f(auto [2]);
void f(auto (&)[2]);
最佳答案
问题确实是decltype(auto)
.返回类型是通过以下列表推导出来的(所讨论的表达式是给 return 语句的表达式):
[dcl.type.decltype] (redacted for emphasis)
decltype-specifier:
decltype ( expression )1 For an expression E, the type denoted by decltype(E) is definedas follows:
- ...
- ...
- otherwise, if E is an unparenthesized id-expression or an unparenthesized class member access ([expr.ref]), decltype(E) is thetype of the entity named by E. If there is no such entity, or if Enames a set of overloaded functions, the program is ill-formed;
- ...
- ...
a
的类型是
int[2]
.所以你本质上是在定义一个具有数组返回类型的函数......但是......
[dcl.fct]
11 Functions shall not have a return type of type array orfunction, although they may have a return type of type pointer orreference to such things. There shall be no arrays of functions,although there can be arrays of pointers to functions.
decltype
.您可以显式返回一个引用(到推导的数组类型),因为这确实是您想要的:
auto& bar() { static int a[2]; return a; }
或者如果你想明确返回类型,而不进入声明者 hell ,你可以以尾随方式指定它:
auto bar() -> int(&)[2] { static int a[2]; return a; }
无论哪种方式,我都会说这比依赖
decltype
的(有时是神秘的)语义要好.当您显式返回左值引用时(当这是您的全部意图时)没有细微的错误。
auto [2]
调整为
auto*
, 而
auto(&) [2]
只绑定(bind)到特定类型的数组。这是一个缩写的函数模板,相当于:
template<typename T>
void f(T[2]);
template<typename T>
void f(T(&)[2]);
返回类型不会像参数那样进行调整。其中参数类型调整本身是 C 传承的东西,从中我们得到了不是一等公民的原始数组。随着类型的发展,它们非常不规则。如果你需要“更清晰”的数组值语义,你有
std::array
正是为了这个目的。
关于c++ - 从用 decltype(auto) 声明的函数返回一个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68509535/
这个问题在这里已经有了答案: Range based loop: get item by value or reference to const? (5 个答案) 关闭 6 年前。 如果我有这样的类
最近,我使用 CSS grid 创建了一个布局.虽然这很好用,但我对它的工作原理感到困惑。具体来说,我对 grid-template-rows: auto auto 1fr auto; 这一行感到困惑
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Why don't margin-top: auto and margin-bottom:auto work
我几乎已经尝试了所有我知道的方法,但是当我将我的 Android studio 更新到最新版本时,它仍然显示此错误。我该怎么办? gradle.build 是: buildscript { r
我想创建一个deep_flatten函数模板,该模板将生成包含range ed的元素的join。例如,如果仅考虑嵌套的std::vector,我可以拥有: template struct is_ve
我刚刚看了 Scott Meyers Universal References in C++11有一件事我不太明白。 我对作为“通用引用”的 auto 之间的区别感到有点困惑,即 auto&& 和常规
这个问题在这里已经有了答案: C++11 Range-based for-loop efficiency "const auto &i" versus "auto i" (3 个答案) 关闭 3 年
由于 auto 关键字在编译时获取类类型,我想知道使用 auto* 是否有任何效率,或者是否有任何特殊用途该表达式,因为 auto 在编译时已经获得了指针类型。 最佳答案 这个“新奇的 C++11”与
请问我是否正确,对函数返回值使用 auto&& 总是比使用 auto 更好。例如,在 auto val = someObj.getVal(); 如果 getVal() 返回引用,则 val 将是一个拷
有区别吗: template constexpr decltype(auto) f(T&& x) -> decltype(std::get(std::forward(x))) { retur
我想创建一个 deep_flatten会产生 range 的函数模板深的元素join编。例如,如果我们只考虑嵌套 std::vector s,我可以有: template struct is_vec
我在玩auto在 std::pair .在下面的代码中,函数 f应该返回 std::pair依赖于模板参数的类型。 一个工作示例: 示例 1 template auto f() { if c
我是一名 Android 开发人员,我正在尝试开发一个定制的 Android Auto 应用程序,它可以简单地镜像手机屏幕。 我知道目前 API 仅适用于音乐和消息应用程序,但我会编写一个应用程序来镜
我有一个很大的 div,里面有文字: #big-div { height: 400px; overflow: auto; } 如何才能使当新内容添加到 div(并发生溢出)时,div
我正在尝试设计一个网站,其中包含一个带有溢出的内容区域:自动和一个动态高度。最好是,我希望能够在 overflow: auto div 下方放置一个页眉和一个页脚,并让该 div 占用剩余的空间,但到
这个问题在这里已经有了答案: Does 'auto' type assignments of a pointer in c++11 require '*'? (3 个答案) 关闭 6 年前。 以下在
当使用 auto&& 处理返回左值的函数时: int func() { int v=42; return v; } auto && v = func(); 将 v 视为引用而不是左值会产生
我读了一篇关于 auto 类型推导的文章,使用 decltype 我想知道我在下面的例子中关于如何推导类型的逻辑是否正确(所以如果我是有误请指正:) #include using namespace
这个问题在这里已经有了答案: What's the semantically accurate position for the ampersand in C++ references (3 个回答)
假设我有 class Container { public: T getValue() const { return t; } const T& getCRef() const {
我是一名优秀的程序员,十分优秀!