<stdio.h> は <cstdio> に、
<string.h> は <cstring> になった。
つまり、".h" が消えて、頭に "c" が付くように。

既存のソフトウェアは名前空間を使う設計になっていない。標準ライブラリを std でラップしたことによって既存のコードが使えなくなったりしたら、はなはだ遺憾なことである (中略) 激怒したプログラマたちによる暴動を恐れた標準化委員会は、 std でラップされたコンポーネントのヘッダには以前とは別の名前を付けることに決めた。

Scott Meyers 著 『Effective C++ 改訂2版』 49項 標準ライブラリに精通しよう (247ページ)

古いスタイルのサンプルコード。


$ cat ./oldstyle.cpp
#include <stdio.h>
 
int main(int argc, char *argv[]){
  printf("hello world\n");
  return 0;
}
 
$ gcc -lstdc++ -o ./oldstyle ./oldstyle.cpp
 
$ ./oldstyle
hello world

新しいスタイルのサンプルコード。


$ cat ./newstyle.cpp
#include <cstdio>
 
int main(int argc, char *argv[]){
  std::printf("hello world\n");
  return 0;
}
 
$ gcc -lstdc++ -o ./newstyle ./newstyle.cpp
 
$ ./newstyle
hello world

今回の実行環境。

Mac OS X Snow Leopard 10.6.8 on MacBook Air


$ uname -mrsv
Darwin 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386
 
$ gcc -v
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5666.3~6/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)

Ref.
- Amazon.co.jp: Effective C++ 【改訂第2版】 アスキーアジソンウェスレイシリーズ―Ascii Addison Wesley programming series: Scott Meyers, 吉川 邦夫: 本
- Amazon.co.jp: Effective C++ 原著第3版 (ADDISON-WESLEY PROFESSIONAL COMPUTING SERIES): スコット・メイヤーズ, 小林 健一郎: 本

tags: cpp namespace

Posted by NI-Lab. (@nilab)