Bia*_*sta 11 c++ c++17 structured-bindings
以下代码应该编译吗?
#include <type_traits>
void foo() {
const std::pair<int, int> x = {1, 2};
auto [a, b] = x;
static_assert(std::is_const_v<decltype(a)>);
static_assert(std::is_const_v<decltype(b)>);
}
Run Code Online (Sandbox Code Playgroud)
那么,这是MSVC错误吗?
该标准是不是直接在这里(我有一个快速浏览一下),但考虑规则auto,我想,a并且b应该被复制丢弃CV-预选赛。
Bar*_*rry 14
以下代码应该编译吗?
它不是。这是一个MSVC错误。
结构化绑定声明引入了一个新名称(仅用于规范)e,该名称的声明方式如下:
auto e = x;
Run Code Online (Sandbox Code Playgroud)
的类型e称为E,并且由于初始值设定项类似于元组,因此绑定的类型由给出tuple_element_t<i, E>。在这种情况下E是pair<int, int>,所以这两种类型都只是int。decltype结构化绑定的规则是给出引用的类型,所以decltype(a)和decltype(b)都是int。
这里的重要部分是a和b(结构化绑定)来自发明的变量(e),而不是其初始值设定项(x)。e不是const因为你刚刚宣布了auto。我们正在做的是复制x,然后将绑定绑定到此(非const)副本中。
您代码中的静态断言应该失败。为什么?因为您的代码与以下情况基本相同:
#include <type_traits>
void foo() {
const int x_1 = 1;
const int x_2 = 2;
auto a = x_1;
auto b = x_2;
static_assert(std::is_const_v<decltype(a)>);
static_assert(std::is_const_v<decltype(b)>);
}
Run Code Online (Sandbox Code Playgroud)
这确实对MSVC确实无法为好。
在C ++中,表达式类型在赋值时衰减:auto看见an int而不是a const int。结构化绑定仅使您一次可以完成多个auto绑定。
...因此,MSVC不会在代码中的断言上失败的事实似乎是一个错误。
| 归档时间: |
|
| 查看次数: |
370 次 |
| 最近记录: |