Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using underscores at the start of a #define for an include guard

Tags:

c++

xcode

This answer points to the reference material stating that you should not use two underscores followed by a capital letter.

Then there is this comment to this question, the first by @metal, that says you also cannot use such a name when creating include guards.

However, I'm curious then why Xcode does exactly that when it automatically creates include guards for new C++ files:

#ifndef __DataSource__File__
#define __DataSource__File__

#include <iostream>

#endif 

This is the standard biolerplate that Xcode places at the top of a new C++ file; in this case, the project is named "DataSource". If the name of the project started with a digit, then Xcode would replace this digit with a third underscore.

If it's illegal for a user to write this, then isn't it illegal for Xcode to write this?

like image 472
johnbakers Avatar asked Mar 25 '23 04:03

johnbakers


1 Answers

If it's illegal for a user to write this, then isn't it illegal for Xcode to write this?

Indeed, although "illegal" is perhaps rather a strong word. If that is the default behaviour, then whoever configured it to generate dodgy include guards didn't know that you shouldn't do that. Sadly, not every software developer (even those making development tools) has complete knowledge of the language and tools they use.

A great many people like to decorate their include guards with weird patterns of underscores, even though they shouldn't. Presumably, they see it done in standard library headers (as it should be, since that's the sort of thing that such names are reserved for) and assume that they should do the same for some reason.

like image 91
Mike Seymour Avatar answered Apr 23 '23 21:04

Mike Seymour