在#define中使用双冒号(::) [英] Use double colons (::) in #define
问题描述
我可以在 #define
中使用双冒号吗?我想保存一些写在实现文件,例如。像这样:
// foo.h
#define模板模板< class T&
#define foo :: foo< T> ::
模板类foo {
T& baz();
};
#includefoo.tpp
#undef template
#undef foo ::
// foo.tpp
template T& ; foo :: baz(){
//做东西。
}
但我得到语法错误我真的不明白。
(请参阅键盘上的示例):
< blockquote>
第11行:错误:在宏名称后缺少空格
第10行:错误:#undef指令末尾的额外标记
第4行:错误:'foo'不是模板
由于-Wfatal-errors而导致编译终止。
否。宏的名称必须是标识符;它不能由其他字符组成,并且不能由多个令牌组成。
#define模板
无效,因为模板
不是标识符,它是一个关键字。
#define foo :: foo< T> ::
在C90和C ++ 98中有效:它定义了一个名为 foo
的宏, c $ c> :: foo< T> :: (这不是你想要做的,但是有效)。但是,在C99和C ++ 11中这是无效的,因为在较新的语言版本中,在类似对象的宏的名称及其替换列表(被替换的令牌)之间必须有空格。 p>
May I use double colons in #define
? I'd like to save some writing in the implementation files, e.g. like this:
// 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.
}
But I get syntax errors I don't really understand. (See an example on 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.
No. The name of a macro must be an identifier; it can't consist of other characters and it can't consist of multiple tokens.
#define template
is invalid because template
is not an identifier, it is a keyword.
#define foo:: foo<T>::
was valid in C90 and C++98: it defines a macro named foo
that is replaced by :: foo<T>::
(that's not what you want to do, but it was valid). However, this is invalid in C99 and C++11 because in the newer revisions of the languages, there must be whitespace between the name of an object-like macro and its replacement list (the tokens with which it is replaced).
这篇关于在#define中使用双冒号(::)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!