error: binding ‘const double’ to reference of type ‘double&’ discards qualifiers
error: binding ‘const double’ to reference of type ‘double&’ discards qualifiers
我收到错误消息
binding ‘const double’ to reference of type ‘double&’ discards qualifiers
编译时:
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;
}
{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;
当然正确答案是:
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;});
}
我收到错误消息
binding ‘const double’ to reference of type ‘double&’ discards qualifiers
编译时:
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;
}
{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;
当然正确答案是:
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;});
}