OSDev and C++0x

Programming, for all ages and all languages.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: OSDev and C++0x

Post by Owen »

-m32 wrote:One thing I think is just frigging stupid:

Code: Select all

nullptr
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. :roll:
How much code would break if they called it either of the above suggestions?
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: OSDev and C++0x

Post by AndrewAPrice »

Owen wrote:
-m32 wrote:One thing I think is just frigging stupid:

Code: Select all

nullptr
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. :roll:
How much code would break if they called it either of the above suggestions?
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.
My OS is Perception.
User avatar
-m32
Member
Member
Posts: 120
Joined: Thu Feb 21, 2008 5:59 am
Location: Ottawa, Canada

Re: OSDev and C++0x

Post by -m32 »

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.
My point is: the name 'null' already implies "null" not 0 (zero). Calling it nullptr is counter-intuitive.

If I'm writing something in C# or Java and I have a statement like this:

Code: Select all

Object o = null;
No one would assume that null here is at all related to the number 0 (zero). It's not like having a keyword of "null" will break any C app which uses the macro "NULL".
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: OSDev and C++0x

Post by Owen »

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.
User avatar
gravaera
Member
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

Post by gravaera »

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.
pcmattman
Member
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

Post by pcmattman »

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?
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.

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?
User avatar
Combuster
Member
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

Post by Combuster »

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...
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
-m32
Member
Member
Posts: 120
Joined: Thu Feb 21, 2008 5:59 am
Location: Ottawa, Canada

Re: OSDev and C++0x

Post by -m32 »

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...
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.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: OSDev and C++0x

Post by Owen »

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...)
luiscubal
Posts: 13
Joined: Tue Jul 28, 2009 3:59 pm

Re: OSDev and C++0x

Post by luiscubal »

If they redefined the meaning of NULL or null, it'd be chaos.
The issue here is about code like:

Code: Select all

void x(int z){ printf("int"); }
void x(void* z){ printf("void*"); }
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.

Code: Select all

#ifdef NULL
#undef NULL
#endif
#define NULL nullptr
This should solve all your problems. :P
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: OSDev and C++0x

Post by AndrewAPrice »

@luiscubal: What if some silly standard made NULL a compiler constant? ;)
My OS is Perception.
User avatar
Rusky
Member
Member
Posts: 792
Joined: Wed Jan 06, 2010 7:07 pm

Re: OSDev and C++0x

Post by Rusky »

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.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: OSDev and C++0x

Post by Love4Boobies »

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.
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.

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 ]
User avatar
gravaera
Member
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

Post by gravaera »

@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.
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: OSDev and C++0x

Post by Solar »

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.
Every good solution is obvious once you've found it.
Post Reply