How much code would break if they called it either of the above suggestions?-m32 wrote:One thing I think is just frigging stupid:
WTF? Every other language uses NULL or null. Are they just trying to defy convention? No sh|t null is used in the context of pointers.Code: Select all
nullptr
OSDev and C++0x
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: OSDev and C++0x
- AndrewAPrice
- Member
- Posts: 2299
- Joined: Mon Jun 05, 2006 11:00 pm
- Location: USA (and Australia)
Re: OSDev and C++0x
Perhaps because in certain situations, 0 is a valid pointer, in which case you can define nullptr to something you would know would be invalid.Owen wrote:How much code would break if they called it either of the above suggestions?-m32 wrote:One thing I think is just frigging stupid:
WTF? Every other language uses NULL or null. Are they just trying to defy convention? No sh|t null is used in the context of pointers.Code: Select all
nullptr
My OS is Perception.
Re: OSDev and C++0x
My point is: the name 'null' already implies "null" not 0 (zero). Calling it nullptr is counter-intuitive.MessiahAndrw wrote:Perhaps because in certain situations, 0 is a valid pointer, in which case you can define nullptr to something you would know would be invalid.
If I'm writing something in C# or Java and I have a statement like this:
Code: Select all
Object o = null;
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: OSDev and C++0x
The fact is, there is lots of code out there which uses null as a variable name, and a tiny amount which uses nullptr as a variable name.
- gravaera
- Member
- Posts: 737
- Joined: Tue Jun 02, 2009 4:35 pm
- Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.
Re: OSDev and C++0x
Hmmm...but should a language be modified unnecessarily to allow for illogical use? What exactly would go through a programmer's mind to name a variable 'null', or 'asm' for instance?
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
-
- Member
- Posts: 2566
- Joined: Sun Jan 14, 2007 9:15 pm
- Libera.chat IRC: miselin
- Location: Sydney, Australia (I come from a land down under!)
- Contact:
Re: OSDev and C++0x
Your point rings true, in a perfect world. This isn't a perfect world, and people do write code which defies logic - according to us, anyway.gravaera wrote:Hmmm...but should a language be modified unnecessarily to allow for illogical use? What exactly would go through a programmer's mind to name a variable 'null', or 'asm' for instance?
Should the language be modified to improve compatibility with existing code, and thus improve the transition experience? Or should it use names such as NULL or null and possibly break lots of existing code?
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: OSDev and C++0x
I don't think you should put backwards compatibility above semantic cleanness. It'd harm the usability of a language to use __null instead of the null as the name of a keyword because someone *might* use the latter in existing code. In this particular case, you'd force people to use more sense in naming which actually is a good idea IMO...
Re: OSDev and C++0x
I agree. Besides, not to insult anyone here, but if you're a programmer who names variables or functions (etc...) something like 'null'... you're just not doing a very good job. I'm sorry. It's true. If you're someone who tries to use null as a variable name, you're probably likely to use other common keywords and won't understand why you get a compiler error when you try to name a variable int, or float.Combuster wrote:I don't think you should put backwards compatibility above semantic cleanness. It'd harm the usability of a language to use __null instead of the null as the name of a keyword because someone *might* use the latter in existing code. In this particular case, you'd force people to use more sense in naming which actually is a good idea IMO...
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: OSDev and C++0x
There are lots of classes which have a logical null (i.e. invalid, or otherwise uninitialized) value. In those cases, I wouldn't be surprised if somewhere a variable called "null" was used to represent this. It perfectly describes the variable - what, would you call it SomeString::nullString, with it's redundancy? I mean, how were programmers supposed to know that they were going to add a null keyword to C++ in the future?
C++ isn't, and has never been a clean language. It's been full of backwards compatibility since it was created, and by all measures, this has been in it's favor. It's another example of purity being sacrificed for utility - the time taken to teach someone the quirks in C++ is less than that which would be taken to teach them both C and some hypothetical C++ with a different syntax, or to rewrite the program in a new language.
It's another example of "worse but compatible" winning (For another, often complained about example, see the 8086 architecture, which is source compatible with all of Intel's proceeding processors, at least through a translator...)
C++ isn't, and has never been a clean language. It's been full of backwards compatibility since it was created, and by all measures, this has been in it's favor. It's another example of purity being sacrificed for utility - the time taken to teach someone the quirks in C++ is less than that which would be taken to teach them both C and some hypothetical C++ with a different syntax, or to rewrite the program in a new language.
It's another example of "worse but compatible" winning (For another, often complained about example, see the 8086 architecture, which is source compatible with all of Intel's proceeding processors, at least through a translator...)
Re: OSDev and C++0x
If they redefined the meaning of NULL or null, it'd be chaos.
The issue here is about code like:
Where x(NULL) prints int instead of void*. They needed to keep that behavior, yet it was weird. nullptr was the best solution they could find.
Either way, here's a solution for you.
This should solve all your problems.
The issue here is about code like:
Code: Select all
void x(int z){ printf("int"); }
void x(void* z){ printf("void*"); }
Either way, here's a solution for you.
Code: Select all
#ifdef NULL
#undef NULL
#endif
#define NULL nullptr
- AndrewAPrice
- Member
- Posts: 2299
- Joined: Mon Jun 05, 2006 11:00 pm
- Location: USA (and Australia)
Re: OSDev and C++0x
@luiscubal: What if some silly standard made NULL a compiler constant?
My OS is Perception.
Re: OSDev and C++0x
I never liked most of C++ anyway, and C++0x is just making things worse. nullptr, decltype, constexpr, mixing the language with STL (only a problem in a supposedly systems-programming language), "uniform" initialization (wooooo another way to do things) and alternate declaration syntax (why not tweak scope like they did with for-local declarations?) are all ugly and/or useless.
However, a few things I do like. Lambdas and type inference are excellent. User-defined literals make implementing custom string classes waaaay nicer- no more preprocessor macros or manual constructors for string literals. Variadic templates and arguments are somewhat better than before (although templates... eugh). Enums are also much improved. The range-based for loop is a cool idea, although maybe they should require containers to explicitly implement the begin/end interface (from what wikipedia says it looks like a duck-typing kind of thing, but Iunno).
Even though using null would cause problems, nullptr is still ugly. I think a lot of uses of null are hacks to get exactly what nullptr is. If nullptr were extended to have a type so that classes could have a null constructor to define their own null values, there would be a lot less need for non-keyword null.
Type inference is great. The compiler is smart enough to know what type something should be most of the time (i.e. when you're not defaulting it to 0 or null), without sacrificing readability. In fact, it often increases maintainability (iteration, new Something definitions, etc.). There's usually no reason for the programmer to manually type in that stuff.
However, a few things I do like. Lambdas and type inference are excellent. User-defined literals make implementing custom string classes waaaay nicer- no more preprocessor macros or manual constructors for string literals. Variadic templates and arguments are somewhat better than before (although templates... eugh). Enums are also much improved. The range-based for loop is a cool idea, although maybe they should require containers to explicitly implement the begin/end interface (from what wikipedia says it looks like a duck-typing kind of thing, but Iunno).
Even though using null would cause problems, nullptr is still ugly. I think a lot of uses of null are hacks to get exactly what nullptr is. If nullptr were extended to have a type so that classes could have a null constructor to define their own null values, there would be a lot less need for non-keyword null.
Type inference is great. The compiler is smart enough to know what type something should be most of the time (i.e. when you're not defaulting it to 0 or null), without sacrificing readability. In fact, it often increases maintainability (iteration, new Something definitions, etc.). There's usually no reason for the programmer to manually type in that stuff.
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: OSDev and C++0x
I'd have to disagree. Splitting the code in 100 pieces just as to avoid using the keyword "goto" is nonsense IMHO. It's not the keyword that's the problem, but the bugs that the programmers might write because of it; in this case I think your solution is more susceptible to bugs.Solar wrote:I think it very much improves readability because it gets rid of the goto. A return, even a break, is very well-defined in where it returns / breaks to. A goto doesn't carry that kind of information, you have to search for the target label.
The reason I'm necro'ing this post is that I've been doing a little reading into the Linux kernel code (which I think we can all agree is considered to be high-quality code). It uses goto's. A lot of them even. Say you grab a bunch of locks and then some error comes up. The cleanest solution is to just goto the place where everything is unlocked again.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
- gravaera
- Member
- Posts: 737
- Joined: Tue Jun 02, 2009 4:35 pm
- Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.
Re: OSDev and C++0x
@Love4Boobies: The code is functional, but I consider it flawed since it is very badly (sparsely) commmented. But the uniformity of the coding style and the general quality of the code is unquestionable.
I'm very surprised that for a large project like the Linux kernel, no-one saw it needful to enforce stricter commenting doctrinology.
--Be well, be healthy, and eat fruit,
gravaera.
I'm very surprised that for a large project like the Linux kernel, no-one saw it needful to enforce stricter commenting doctrinology.
--Be well, be healthy, and eat fruit,
gravaera.
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
Re: OSDev and C++0x
But... but... good code doesn't need comments, does it?
(Anyone finding the dripping sarcasm in the comment above may keep it. )
Edit: I just took the time and actually read the Wikipedia article on C++0x. My first impression: Awesome. C++ doesn't get one bit more beautiful with it, but I doubt any other language can touch it for sheer, raw power and versatility.
(Anyone finding the dripping sarcasm in the comment above may keep it. )
Edit: I just took the time and actually read the Wikipedia article on C++0x. My first impression: Awesome. C++ doesn't get one bit more beautiful with it, but I doubt any other language can touch it for sheer, raw power and versatility.
Every good solution is obvious once you've found it.