我可以在 #define
中使用双冒号吗?我想在实现文件中保存一些文字,例如像这样:
// foo.h
#define template template <class T>
#define foo:: foo<T>::
template class foo {
T& baz();
};
#include "foo.tpp"
#undef template
#undef foo::
// foo.tpp
template T& foo::baz() {
// do stuff.
}
但是我遇到了我不太理解的语法错误。 (参见 codepad 上的示例):
Line 11: error: missing whitespace after the macro name
Line 10: error: extra tokens at end of #undef directive
Line 4: error: 'foo' is not a template
compilation terminated due to -Wfatal-errors.
最佳答案
没有。宏的名称必须是标识符;它不能由其他字符组成,也不能由多个标记组成。
#define template
无效,因为 template
不是标识符,而是关键字。
#define foo:: foo<T>::
在 C90 和 C++98 中有效:它定义了一个名为 foo
的宏替换为 :: foo<T>::
(这不是您想要做的,但它是有效)。但是,这在 C99 和 C++11 中是无效的,因为在这些语言的较新版本中,类对象宏的名称与其替换列表(替换它的标记)之间必须有空格。
关于c++ - 在#define 中使用双冒号 (::),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7863467/