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

Why does constant CLOCKS_PER_SEC not need to be namespaced with std:: but <ctime> clock() and clock_t do?

本文关键字:clock 常量 gt ctime CLOCKS PER std 不需要 命名 lt 为什么      更新时间:2023-10-16

我正在尝试<ctime>库,以获得类型的clock_tclock()功能和常数CLOCKS_PER_SEC。我注意到我必须使用std::命名clock_tclock(),而不是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;
}

CLOCKS_PER_SEC(以及所有大写字母中的大多数名称(是预处理器宏。宏不参与C 名称空间系统,因为使用它们的代码与C不兼容,这当然没有名称空间。