相关疑难解决方法(0)

使用结构化绑定标记为const的变量不是const

我一直在编写一组类来允许一个简单的类似python的zip函数.以下代码片段(几乎)可以正常工作.然而,这两个变量ab没有const.

std::vector<double> v1{0.0, 1.1, 2.2, 3.3};
std::vector<int> v2{0, 1, 2};

for (auto const& [a, b] : zip(v1, v2))
{
    std::cout << a << '\t' << b << std::endl;
    a = 3; // I expected this to give a compiler error, but it does not
    std::cout << a << '\t' << b << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我一直在使用gcc 7.3.0.这是MCVE:

#include <iostream>
#include <tuple>
#include <vector>

template <class ... Ts>
class zip_iterator
{
    using value_iterator_type = …
Run Code Online (Sandbox Code Playgroud)

c++ const c++17 structured-bindings

19
推荐指数
1
解决办法
939
查看次数

标签 统计

c++ ×1

c++17 ×1

const ×1

structured-bindings ×1