Where:nullptr emulation is already in heavy use within Blink, but its use is not allowed beyond there because of issues with ODR and emulating nullptr in Chromium.
What:Allowing/Encouraging the use of nullptr (as opposed to NULL)Why:In C++ < 11, NULL may be an integral type (0) or a pointer type (void*)0, although the world is messy and full of lies and disappointment.In C++11, nullptr is a distinguishable type, which can be used, for example, in template specializations to distinguish between integers, pointer types, and explicit nullptr.With nullptr support, one could update scoped_ptr<> to handle implicit initialization from nullptr, so that the following code:scoped_ptr<Foo> GetFoo() {
return scoped_ptr<Foo>();
}becomes
scoped_ptr<Foo> GetFoo() {
return nullptr;
}
On Tue, Sep 23, 2014 at 2:05 PM, Ryan Sleevi <rsl...@chromium.org> wrote:What:Allowing/Encouraging the use of nullptr (as opposed to NULL)Why:In C++ < 11, NULL may be an integral type (0) or a pointer type (void*)0, although the world is messy and full of lies and disappointment.In C++11, nullptr is a distinguishable type, which can be used, for example, in template specializations to distinguish between integers, pointer types, and explicit nullptr.With nullptr support, one could update scoped_ptr<> to handle implicit initialization from nullptr, so that the following code:scoped_ptr<Foo> GetFoo() {
return scoped_ptr<Foo>();
}becomes
scoped_ptr<Foo> GetFoo() {
return nullptr;
}I strongly support this proposal, but haven't figured out how to make this work with our C++03 move emulation code for scoped_ptr<T>. Folks smarter than me are pretty sure this can be done but I haven't seen it yet. Could you link to a patch that works for this and move emulation?
On Tue, Sep 23, 2014 at 2:13 PM, James Robinson <jam...@chromium.org> wrote:On Tue, Sep 23, 2014 at 2:05 PM, Ryan Sleevi <rsl...@chromium.org> wrote:What:Allowing/Encouraging the use of nullptr (as opposed to NULL)Why:In C++ < 11, NULL may be an integral type (0) or a pointer type (void*)0, although the world is messy and full of lies and disappointment.In C++11, nullptr is a distinguishable type, which can be used, for example, in template specializations to distinguish between integers, pointer types, and explicit nullptr.With nullptr support, one could update scoped_ptr<> to handle implicit initialization from nullptr, so that the following code:scoped_ptr<Foo> GetFoo() {
return scoped_ptr<Foo>();
}becomes
scoped_ptr<Foo> GetFoo() {
return nullptr;
}I strongly support this proposal, but haven't figured out how to make this work with our C++03 move emulation code for scoped_ptr<T>. Folks smarter than me are pretty sure this can be done but I haven't seen it yet. Could you link to a patch that works for this and move emulation?I had a patch somewhere that did this within the constraints of our C++03 code, but would have required base::nullptr, which was the kiss of death for it. It's potentially buried in my email somewhere, but may have been deleted. I'm sure someone smarter than me on codereview could find it, since I had uploaded it for discussion.But yes, it is indeed possible.
Also be aware that we can't use std::nullptr_t yet, since that's a library feature, but we could allow decltype(nullptr) (which is the same thing).- JamesWhere:nullptr emulation is already in heavy use within Blink, but its use is not allowed beyond there because of issues with ODR and emulating nullptr in Chromium.
--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
On Tue, Sep 23, 2014 at 5:24 PM, Ryan Sleevi <rsl...@chromium.org> wrote:On Tue, Sep 23, 2014 at 2:13 PM, James Robinson <jam...@chromium.org> wrote:On Tue, Sep 23, 2014 at 2:05 PM, Ryan Sleevi <rsl...@chromium.org> wrote:What:Allowing/Encouraging the use of nullptr (as opposed to NULL)Why:In C++ < 11, NULL may be an integral type (0) or a pointer type (void*)0, although the world is messy and full of lies and disappointment.In C++11, nullptr is a distinguishable type, which can be used, for example, in template specializations to distinguish between integers, pointer types, and explicit nullptr.With nullptr support, one could update scoped_ptr<> to handle implicit initialization from nullptr, so that the following code:scoped_ptr<Foo> GetFoo() {
return scoped_ptr<Foo>();
}becomes
scoped_ptr<Foo> GetFoo() {
return nullptr;
}I strongly support this proposal, but haven't figured out how to make this work with our C++03 move emulation code for scoped_ptr<T>. Folks smarter than me are pretty sure this can be done but I haven't seen it yet. Could you link to a patch that works for this and move emulation?I had a patch somewhere that did this within the constraints of our C++03 code, but would have required base::nullptr, which was the kiss of death for it. It's potentially buried in my email somewhere, but may have been deleted. I'm sure someone smarter than me on codereview could find it, since I had uploaded it for discussion.But yes, it is indeed possible.I believe it's here https://codereview.chromium.org/11411318/
Indeed, that's the one! With ubiquitous nullptr support, it'd also be a lot less complicated (note that some of the overhead came from the nullptr emulation)
For the user-visible effects, I mostly just anticipate this being one less hurdle when converting code to take/receive a scoped_ptr<T> over a T*, since typing scoped_ptr<T>() is annoying.
Is it helpful for EXPECT_EQ, CHECK_EQ, etc? I seem to remember there are problems using NULL with those.
This is where patches are helpful :). Here's the comment from base/logging.h:// WARNING: These may not compile correctly if one of the arguments is a pointer// and the other is NULL. To work around this, simply static_cast NULL to the// type of the desired pointer.and here's some code that uses it:base/logging_unittest.cc:196: DCHECK_EQ(mock_log_source.Log(), static_cast<const char*>(NULL))Could you get rid of the need for the static_cast<const char*> there? Probably - send us a patch showing how!- James
On Tue, Sep 23, 2014 at 3:49 PM, Matt Mueller <ma...@chromium.org> wrote:Is it helpful for EXPECT_EQ, CHECK_EQ, etc? I seem to remember there are problems using NULL with those.Yes; due to the way those templates work, you generally can't do:EXPECT_EQ(NULL, foo());
gtest's source itself can't use c++11. Libraries using gtest can.
On Tue, Sep 23, 2014 at 3:55 PM, Nico Weber <tha...@chromium.org> wrote:gtest's source itself can't use c++11. Libraries using gtest can.Incidentally, I think upstream gtest and gmock are actually C++11 at this point -- I tried to bump us to the latest versions a few months ago and the compile broke due to them using C++11 features. I've been waiting on the C++11 OK to be able to rev these.
--
On Tue, Sep 23, 2014 at 2:05 PM, Ryan Sleevi <rsl...@chromium.org> wrote:What:Allowing/Encouraging the use of nullptr (as opposed to NULL)Why:In C++ < 11, NULL may be an integral type (0) or a pointer type (void*)0, although the world is messy and full of lies and disappointment.In C++11, nullptr is a distinguishable type, which can be used, for example, in template specializations to distinguish between integers, pointer types, and explicit nullptr.With nullptr support, one could update scoped_ptr<> to handle implicit initialization from nullptr, so that the following code:scoped_ptr<Foo> GetFoo() {
return scoped_ptr<Foo>();
}becomes
scoped_ptr<Foo> GetFoo() {
return nullptr;
}I strongly support this proposal, but haven't figured out how to make this work with our C++03 move emulation code for scoped_ptr<T>. Folks smarter than me are pretty sure this can be done but I haven't seen it yet. Could you link to a patch that works for this and move emulation?Also be aware that we can't use std::nullptr_t yet, since that's a library feature, but we could allow decltype(nullptr) (which is the same thing).
- JamesWhere:nullptr emulation is already in heavy use within Blink, but its use is not allowed beyond there because of issues with ODR and emulating nullptr in Chromium.
--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
On Tue, Sep 23, 2014 at 2:13 PM, James Robinson <jam...@chromium.org> wrote:On Tue, Sep 23, 2014 at 2:05 PM, Ryan Sleevi <rsl...@chromium.org> wrote:What:Allowing/Encouraging the use of nullptr (as opposed to NULL)Why:In C++ < 11, NULL may be an integral type (0) or a pointer type (void*)0, although the world is messy and full of lies and disappointment.In C++11, nullptr is a distinguishable type, which can be used, for example, in template specializations to distinguish between integers, pointer types, and explicit nullptr.With nullptr support, one could update scoped_ptr<> to handle implicit initialization from nullptr, so that the following code:scoped_ptr<Foo> GetFoo() {
return scoped_ptr<Foo>();
}becomes
scoped_ptr<Foo> GetFoo() {
return nullptr;
}I strongly support this proposal, but haven't figured out how to make this work with our C++03 move emulation code for scoped_ptr<T>. Folks smarter than me are pretty sure this can be done but I haven't seen it yet. Could you link to a patch that works for this and move emulation?Also be aware that we can't use std::nullptr_t yet, since that's a library feature, but we could allow decltype(nullptr) (which is the same thing).Can it make sense to have a typedef decltype(nullptr) nullptr_t; in the (say) base namespace, until we can use std::nullptr_t?Antoine
Can it make sense to have a typedef decltype(nullptr) nullptr_t; in the (say) base namespace, until we can use std::nullptr_t?
On Tue, Sep 23, 2014 at 5:09 PM, Antoine Labour <pi...@google.com> wrote:Can it make sense to have a typedef decltype(nullptr) nullptr_t; in the (say) base namespace, until we can use std::nullptr_t?
AntoineThe only reason we should need the nullptr_t type would be for template disambiguation.This assumes that decltype(nullptr) is legal (although not necessarily any decltype), in part, because it's what we've been doing in Blink for some time (which decltype's into... std::nullptr_t :'( )
Blink is already using nullptr extensively via emulation. This would have relatively small impact on most of the Blink codebase except for in places where the WTF emulation can't be used, such as the Blink public c++ api, where the impact will probably be fairly small.
- James
* We won't mass-replace NULL with nullptr (only one person asked for this, it makes blame output harder to parse, and lines containing NULL are often tricky and interesting in blame output).
(Another issue that I just learned about that makes this difficult is that the Windows code uses NULL heavily in places where no pointer is expected – several of the HANDLE types (not HANDLE itself I believe, but e.g. TRACEHANDLE), and MSDN even says to use NULL for those (http://msdn.microsoft.com/en-us/library/windows/desktop/aa363686(v=vs.85).aspx). This would probably make a mass find-and-replace somewhat involved too.)
--
In this case, maybe we can have a clang plugin/reformatter that identifies places where NULL is actually used as a pointer (like when it is immediately implicitly converted)... and maybe also cases where it is used as a sentinel vararg :). This wouldn't get all cases, but would probably get most while still being reasonable safe.
PK
If we can do a machine conversion I think we should, but I'm not entirely sure this will be easy. I don't want to block usage of the feature on their being a good rewriter. For example, we'll have to reformat many lines of code since nullptr is 3 characters longer which might be tricky in parts of the codebase that aren't currently clang-formatted.How about we allow nullptr, encourage conversion, and if somebody cooks up a workable translator we allow using it but we do not make that a condition of use of the feature?- James
On Wed, Sep 24, 2014 at 2:15 PM, Chris Hopman wrote:
I think I agree with Peter. As we introduce new features, particularly when we are going to say that you should use the new way and not the old way, I think we should prefer to mass rewrite things to use the new way, even if it comes at some cost in churn/blameability. That being said, for cases where the conversion is complicated, then just a having a policy of converting code as it is changed/introduced/etc may be fine.In this case, maybe we can have a clang plugin/reformatter that identifies places where NULL is actually used as a pointer (like when it is immediately implicitly converted)... and maybe also cases where it is used as a sentinel vararg :). This wouldn't get all cases, but would probably get most while still being reasonable safe.
On Wed, Sep 24, 2014 at 2:02 PM, 'Peter Kasting' via Chromium-dev wrote:
TRACEHANDLE is ULONG64, so that one won't compile after the replace. Besides, we should probably replace it with =0 instead. I did a quick look, there looks to only be a few places where it needs to be fixed, so a mass replace, compile, and fix should be fine for this one.
If we can do a machine conversion I think we should, but I'm not entirely sure this will be easy. I don't want to block usage of the feature on their being a good rewriter. For example, we'll have to reformat many lines of code since nullptr is 3 characters longer which might be tricky in parts of the codebase that aren't currently clang-formatted.
How about we allow nullptr, encourage conversion, and if somebody cooks up a workable translator we allow using it but we do not make that a condition of use of the feature?
clang-modernize has a transform for this: http://clang.llvm.org/extra/UseNullptrTransform.html