以下计划
#include <ctime>
struct clock {};
int main() {
clock c;
}
无法在g ++ 5.4和clang 3.8(Ubuntu 64位)上进行编译。
clock.cpp: In function ‘int main()’:
clock.cpp:6:11: error: expected ‘;’ before ‘c’
clock c;
^
clock.cpp:6:5: error: must use 'struct' tag to refer to type 'clock' in this scope
clock c;
^
struct
/usr/include/time.h:189:16: note: struct 'clock' is hidden by a non-type declaration of 'clock' here
extern clock_t clock (void) __THROW;
^
1 error generated.
诊断在形式上有所不同,但与同一问题有关。与标准C函数clock
发生冲突,并且程序中定义了相同名称的结构。来自time.h
的相关声明:
extern clock_t clock (void) __THROW;
问题是:这些符号不应该在std
命名空间中,因为该程序包含<ctime>
?有趣的是,在一个读取__BEGIN_NAMESPACE_STD
的宏之后,这个声明只有几行。此外,在<ctime>
中,可以看到:
namespace std
{
using ::clock_t;
using ::time_t;
using ::tm;
using ::clock;
...
}
这里有什么问题吗?
谢谢。
答案 0 :(得分:3)
问题是:这些符号不应该在
std
命名空间中......
是的,他们是。不幸的是,C ++标准还允许实现将来自C库派生头的名称放在全局命名空间中。在这种情况下,您会获得std::clock
和 ::clock
。
这适用于C中具有相应<c*>
版本的所有<*.h>
C ++标头。