Headers Security Issues in C ++

I am new to writing my own headlines, but because of the need I have to learn.

I am writing a headline and I am trying to find heading advocates. Is there a difference between having one or two underscores before and after the included header file?

Take this hypothetical example: xh

//x.h
#ifndef __X_H_INCLUDED__
#define __X_H_INCLUDED__
//functions n stuff
#endif

vs

//x.h
#ifndef _X_H_INCLUDED_
#define _X_H_INCLUDED_
//functions n stuff
#endif

Is this more or less correct than the other? Is there any difference?

+4
source share
3 answers

According to C++11 17.6.4.3.2 Global names(although this restriction has been around for a while):

Certain sets of names and function signatures are always reserved for implementation:

  • , _ _ , , .
  • , , .

, , , , , , .

"" - GUARD_X_H X_H_INCLUDED, , , . Java- , :

AU_COM_POWERFIELD_DATASTRUCTURES_TREES_BALANCED_BTREE_H

( 1024 ).

, ( , ), #pragma once, , .

+8

++ 2003 ( ):

17.4.3.2.1 [lib.global.names]

:

, (_ _) , (2.11), . , namespace.165 165) :: std (17.4.3.1).

+2

, .

#ifndef PROJECT_PATH_TO_FILE_HPP
// etc. etc.

, "", - taste/the/rainbow.hpp, :

#ifndef SKITTLES_TASTE_THE_RAINBOW_HPP
// etc. etc.

, . , foo_bar.hpp, - foo/bar.hpp. , .

Another option is to use namespaces instead of paths:

#define PROJECT_NAMESPACE_MODULE_HPP

I also saw people adding the date and time the file was created or using UUIDs to add extra security against name conflicts.

+1
source

Source: https://habr.com/ru/post/1610675/


All Articles