2

我收到错误

将“const double”绑定到“double&”类型的引用会丢弃限定符

编译时:

g++ -std=c++11 main.cpp
main.cpp: In function ‘Point square(const Point&)’:
main.cpp:14:28: error: binding ‘const double’ to reference of type ‘double&’ discards qualifiers
  for(double &a:{Q.x,Q.y,Q.z})
                            ^

虽然网上还有其他关于这个错误的问题,但我正在寻找这个特定代码的解决方案。我坚持使用ranged for。

#include <iostream>
#include <vector>

class Point
{
public:
    double x,y,z;
};

Point square(const Point &P)
{
    Point Q=P;
    for(double &a:{Q.x,Q.y,Q.z})
        a*=a;
    return Q;
}

int main()
{
    Point P{0.1,1.0,10.0};
    Point Q=square(P);
    std::cout<<"----------------"<<std::endl;
    std::cout<<"Q.x: "<<Q.x<<std::endl;
    std::cout<<"Q.y: "<<Q.y<<std::endl;
    std::cout<<"Q.z: "<<Q.z<<std::endl;
    std::cout<<"----------------"<<std::endl;
    return 0;
}
4

2 回答 2

6

{Q.x,Q.y,Q.z}在你的上下文中创建的初始化列表for仍然基于一个单独的值数组。即使您以某种方式设法修改了这些值,它仍然不会影响您的Q,这显然是您的意图。但是无论如何您都无法修改它们,因为该数组由const元素组成(这是编译器告诉您的)。

如果您想要远程for,可以使用旧的 C 时代技巧

for (double *a : { &Q.x, &Q.y, &Q.z })
  *a *= *a;

或者,或者,

for (auto a : { std::ref(Q.x), std::ref(Q.y), std::ref(Q.z) })
  a *= a;
于 2018-01-09T03:40:36.270 回答
0

当然正确答案是:

for_each(std::tie(x, y, z), [](auto& a){a *= a;});

定义如下:

template <typename Tuple, typename F, std::size_t ...Indices>
void for_each_impl(Tuple&& tuple, F&& f, std::index_sequence<Indices...>) {
    using swallow = int[];
    (void)swallow{1,
        (f(std::get<Indices>(std::forward<Tuple>(tuple))), void(), int{})...
    };
}
template <typename Tuple, typename F>
void for_each(Tuple&& tuple, F&& f) {
    constexpr std::size_t N = std::tuple_size<std::remove_reference_t<Tuple>>::value;
    for_each_impl(std::forward<Tuple>(tuple), std::forward<F>(f),
                  std::make_index_sequence<N>{});
}

int main(){
    double x, y, z;
    for_each(std::tie(x, y, z), [](auto& a){a *= a;});
}

参考:https ://codereview.stackexchange.com/a/67394/82510

于 2018-01-09T04:07:18.337 回答