C++標準ライブラリに含まれるC標準ライブラリヘッダ*1と、std
名前空間との関係についてメモ。
C++11以降では、下記の振る舞いが保証される:
- ヘッダ
cxxx
: 名前空間std
にCライブラリの識別子が宣言される。グローバル名前空間にも宣言されるかもしれない。 - ヘッダ
xxx.h
: グローバル名前空間にCライブラリの識別子が宣言される。名前空間std
にも宣言されるかもしれない。
C++17
C++17 D.5/p3-4より引用(下線部は強調)。29.9.5=Mathematical special functions*2, 21.2.5=byte
type operations*3
3 Every other C header, each of which has a name of the form
name.h
, behaves as if each name placed in the standard library namespace by the correspondingcname
header is placed within the global namespace scope, except for the functions described in 29.9.5, the declaration ofstd::byte
(21.2.1), and the functions and function templates described in 21.2.5. It is unspecified whether these names are first declared or defined within namespace scope (6.3.6) of the namespacestd
and are then injected into the global namespace scope by explicit using-declarations (10.3.3).
4 [Example: The header<cstdlib>
assuredly provides its declarations and definitions within the namespacestd
. It may also provide these names within the global namespace. The header<stdlib.h>
assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespacestd
. -- end example]
C++11/14
C++11 D.5/p2-3より引用(下線部は強調)。C++14でも同様。
2 Every C header, each of which has a name of the form
name.h
, behaves as if each name placed in the standard library namespace by the correspondingcname
header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespacestd
and are then injected into the global namespace scope by explicit using-declarations (7.3.3).
3 [Example: The header<cstdlib>
assuredly provides its declarations and definitions within the namespacestd
. It may also provide these names within the global namespace. The header<stdlib.h>
assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespacestd
. -- end example]
C++03
ヘッダcxxx
では名前空間std
配下に宣言し、ヘッダxxx.h
では “名前空間std
配下に宣言してからグローバル名前空間へ取り込む(using-declaration)” と強く規定されていた。C++03 D.5/p2-3より引用。
2 Every C header, each of which has a name of the form
name.h
, behaves as if each name placed in the Standard library namespace by the correspondingcname
header is also placed within the namespace scope of the namespacestd
and is followed by an explicit using-declaration (7.3.3).
3 [Example: The header<cstdlib>
provides its declarations and definitions within the namespacestd
. The header<stdlib.h>
makes these available also in the global namespace, much as in the C Standard. -- end example]
関連URL
- C++ Standard Library Defect Report List, 456. Traditional C header files are overspecified
- Why < cstdlib > is more complicated than you might think - Red Hat Developer