简体   繁体   English

(void *)1是什么意思?

[英]What does (void *)1 mean?

I'm reading the code of ROS . 我正在阅读ROS的代码。

In the file ros_comm/roscpp/include/ros/subscriber.h , I see such a piece of code: 在文件ros_comm/roscpp/include/ros/subscriber.h ,我看到这样的一段代码:

operator void*() const { return (impl_ && impl_->isValid()) ? (void*)1 : (void*)0; }

Well, (void *)0 can be regarded as NULL in C, but what does (void *)1 mean? 好吧, (void *)0在C语言中可以视为NULL ,但是(void *)1是什么意思?

If a class Foo contains this function, it means that we can code like this: 如果Foo类包含此函数,则意味着我们可以这样编写代码:

Foo foo;
void *ptr = foo;

Right? 对? So does it mean that void *ptr = (void *)1 is possible? 那么这是否意味着void *ptr = (void *)1是可能的? What does this mean? 这是什么意思?

This is an old trick to avoid problems with implicit conversions to bool from before explicit contextual conversions were introduced in C++11. 这是避免在C ++ 11中引入explicit上下文转换之前从bool进行隐式转换的问题的老技巧。 It's intended to be used to check validity: 它旨在用于检查有效性:

Subscriber my_subscriber = someFunction();
if (!my_subscriber) {
    // error case
}

The important point is that no built-in conversion exists from void* to integer types, but one does exist from bool to integer types. 重要的一点是,不存在从void*到整数类型的内置转换,但是确实存在从bool到整数类型的转换。 At the same time, a built-in conversion from void* to bool exists. 同时,存在从void*bool的内置转换。 That means that if you define an implicit conversion to bool , then the following is surprisingly valid: 这意味着,如果您定义了对bool的隐式转换,则以下内容令人惊讶地有效:

void my_func(int i);

void another_func() {
    Subscriber sub = something();
    my_func(sub);
}

Defining a conversion to void* avoids that issue. 将转换定义为void*可避免该问题。


These days that trick is obsolete though. 如今,这种技巧已经过时了。 C++11 introduced explicit conversions. C ++ 11引入了explicit转换。 explicit conversions to bool are considered in the conditions of if and loops, but aren't considered in other problematic cases. if和loop的条件下考虑对bool explicit转换,但在其他有问题的情况下则不考虑。 That means that these days that conversion should be written as: 这意味着这些天的转换应写为:

explicit operator bool() const { return impl_ && impl_->isValid(); }

It shows that either the person who wrote the code isn't very well acquainted with the language or tools they're using, or the code has been around for a long, long time and been hacked on by different people, presumably having undergone a C-to-C++ transition at some time in the past, still carrying some legacy API contract (expecting a void* ) which may be troublesome to change. 它表明,编写代码的人对他们使用的语言或工具不是很熟悉,或者代码已经存在了很长时间,并且被不同的人破解,大概是经过在过去的某个时间从C到C ++的过渡,仍然带有一些旧的API合同(预期为void* ),可能难以更改。

There is no good reason to do such a thing, if you look at the source. 如果您查看源代码,则没有充分的理由这样做。 impl_ is a boost::shared_ptr<Impl> which implements operator bool , and Impl::isValid returns bool , too. impl_是实现operator boolboost::shared_ptr<Impl> ,而Impl::isValid返回bool There's no reason to use, or return anything but bool anywhere. 没有理由使用,或者在任何地方返回bool

Basically, this is a contorted (and possibly dangerous) way of writing: 基本上,这是一种扭曲的(并且可能是危险的)书写方式:

return impl_ && impl_->isValid();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM