为什么常量CLOCKS_PER_SEC不需要用std ::命名空间,但是<ctime> clock()和clock_t呢?

her*_*ity 1 c++ c++11 c++14

我正在尝试使用<ctime>库来获得类型感clock_t,clock()函数和常量CLOCKS_PER_SEC.我注意到,我会命名空间都clock_tclock()std::但不CLOCKS_PER_SEC.这是为什么?如何CLOCKS_PER_SEC自己浮动?

#include <ctime>
#include <iostream>

int main() {
  std::clock_t start;
  double duration;

  start = std::clock();

  for (long int i = 0; i < 10000000000; i ++){
    // do something
  }

  duration = ( clock() - start ) / (double) CLOCKS_PER_SEC;

  std::cout << duration << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

小智 9

CLOCKS_PER_SEC(以及全部大写的大多数其他名称)是预处理器宏.宏不参与C++命名空间系统,因为如果它们这样做,使用它们的代码将与C不兼容,C当然没有命名空间.

  • @heretoinfinity:将"as"读作"因为". (3认同)