[英]Const correctness advice
I have a function that receives a const reference and I need to call a template library function using this reference: 我有一个接收const引用的函数,我需要使用此引用调用模板库函数:
std::vector<cv::Size> get_resolutions(const rs2::device& dev)
{
auto sensor = dev.first<rs2::depth_sensor>();
//more code
}
class device
{
public:
template<class T>
T first()
{
for (auto&& s : query_sensors())
{
if (auto t = s.as<T>()) return t;
}
throw rs2::error("Could not find requested sensor type!");
}
//more code
};
When I compile with gcc I get this error: 当我使用gcc编译时,我收到此错误:
error: passing 'const rs2::device' as 'this' argument discards qualifiers [-fpermissive] 错误:将'const rs2 :: device'作为'this'参数传递丢弃限定符[-fpermissive]
I can't change the first() function as it's part of a external library (librealsense, line 51 in here ). 我无法更改first()函数,因为它是外部库的一部分(librealsense, 这里的第51行)。 I can't remove the const from the function argument dev because that will result in removing const correctness in a lot of places.
我无法从函数参数dev中删除const,因为这将导致在很多地方删除const正确性。
I can overcome the error by removing the const from dev: 我可以通过从dev中删除const来克服错误:
auto sensor = const_cast<rs2::device&>(dev).first<rs2::depth_sensor>();
However, this feels bad practice. 然而,这感觉很糟糕。 Is there any more correct way of dealing with this error?
有没有更正确的方法来处理这个错误? I have tried unsuccessfully the following variations:
我尝试过以下变化但未成功:
auto sensor = dev.first<const rs2::depth_sensor>();
auto sensor = const_cast<const rs2::depth_sensor>(dev.first<rs2::depth_sensor>());
but I get the same error with them. 但我和他们有同样的错误。
I think there are two possible solutions to this. 我认为有两种可能的解决方案。 Either you allow
get_resolutions
to take dev
by non-const reference (although that may require you to modify code at the call site), or you re-implement first
yourself. 您可以允许
get_resolutions
通过非const引用获取dev
(尽管这可能需要您在调用站点修改代码),或者您自己first
重新实现。
Just replace 只需更换
std::vector<cv::Size> get_resolutions(const rs2::device& dev)
with 同
std::vector<cv::Size> get_resolutions(rs2::device& dev)
This, however, would also mean that you can no longer call get_resolutions
with a temporary object. 但是,这也意味着您不能再使用临时对象调用
get_resolutions
。
Looking at the source of the library , however, I really can't see why first()
is non-const. 然而,看看库的来源 ,我真的不明白为什么
first()
是非const的。 All it does is call query_sensors()
(which is const-qualified, and also public), and process the results: 1 它所做的只是调用
query_sensors()
(它是 const限定的,也是公共的),并处理结果: 1
template<class T>
T first()
{
for (auto&& s : query_sensors())
{
if (auto t = s.as<T>()) return t;
}
throw rs2::error("Could not find requested sensor type!");
}
This might be the option with the lowest impact: Just define a first()
yourself, outside of the library, that replicates this functionality: 这可能是影响最小的选项:只需在库外部定义
first()
,即复制此功能:
template <class T>
T custom_first(const rs2::device& dev)
{
for (auto&& s : dev.query_sensors())
if (auto t = s.as<T>())
return t;
throw rs2::error("Could not find requested sensor type!");
}
1 Time to file a bug report, maybe? 1可能是时候提交错误报告?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.