理解带有常量的预处理器宏
Understanding preprocessor macros with constants
我正在试验 C++ 反射方法,但在理解预处理器宏时遇到问题。例如,以下代码有效。
header.h:
#define META_PROPERTY(NAME, TYPE, ACCESS, MIN, MAX) \
class NAME##_MetaProperty : public sge::GetSet<TYPE> \
{ \
public: \
NAME##_MetaProperty(TYPE *NAME) \
: GetSet(NAME, ACCESS) \
, min_(MIN) \
, max_(MAX) \
{} \
. . . other methods . . .
private: \
TYPE min_; \
TYPE max_; \
}NAME##_prop(&NAME); \
main.cpp
main
{
uint32 u(255);
META_PROPERTY(u, uint32, ACCESS_DEFAULT, 0, 1024);
...
}
尽管构造函数不完整,宏还是愉快地创建了 NAME##_MetaProperty 对象,我想我理解为什么预处理器只是填写 MIN 和 MAX 参数。但是,如果我将构造函数更改为以下内容,则会出现一堆编译错误。
public: \
NAME##_MetaProperty(TYPE *NAME, TYPE MIN, TYPE MAX) \
: GetSet(NAME, ACCESS) \
, min_(MIN) \
, max_(MAX) \
{} \
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2143: syntax error: missing ')' before 'constant'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2143: syntax error: missing ';' before 'constant'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2059: syntax error: 'constant'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2059: syntax error: ')'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2334: unexpected token(s) preceding ':'; skipping apparent function body
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2059: syntax error: '&'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): warning C4183: 'u_prop': missing return type; assumed to be a member function returning 'int'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(45): error C2059: syntax error: '='
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(45): error C2238: unexpected token(s) preceding ';'
如果我预定义常量,我会得到另一组更小的错误。
main
{
const uint32 min(0);
const uint32 max(1024);
META_PROPERTY(u, uint32, ACCESS_DEFAULT, min, max);
}
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2664: 'main::u_MetaProperty::u_MetaProperty(const main::u_MetaProperty &)': cannot convert argument 1 from 'uint32 *' to 'const main::u_MetaProperty &'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): note: Reason: cannot convert from 'uint32 *' to 'const main::u_MetaProperty'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): note: No constructor could take the source type, or constructor overload resolution was ambiguous
我只是想了解为什么我不能通过构造函数传递 MIN 和 MAX。这个宏对我的常量做了什么?
您更改 c'tor 的方式使其扩展为
u_MetaProperty(uint32 *u, uint32 0, uint32 1024)
这些是无效的标识符。
如果您真的坚持将 MAX 和 MIN 作为构造函数参数传递,那么您可以这样做:
#define META_PROPERTY(NAME, TYPE, ACCESS, MIN, MAX) \
class NAME##_MetaProperty : public sge::GetSet<TYPE> \
{ \
public: \
NAME##_MetaProperty(TYPE *NAME, TYPE min, TYPE max) \
: GetSet(NAME, ACCESS) \
, min_(min) \
, max_(max) \
{} \
. . . other methods . . .
private: \
TYPE min_; \
TYPE max_; \
}NAME##_prop(&NAME, MIN, MAX); \
但是由于它们应该是编译时常量,宏已经很好地完成了它的工作。
我正在试验 C++ 反射方法,但在理解预处理器宏时遇到问题。例如,以下代码有效。
header.h:
#define META_PROPERTY(NAME, TYPE, ACCESS, MIN, MAX) \
class NAME##_MetaProperty : public sge::GetSet<TYPE> \
{ \
public: \
NAME##_MetaProperty(TYPE *NAME) \
: GetSet(NAME, ACCESS) \
, min_(MIN) \
, max_(MAX) \
{} \
. . . other methods . . .
private: \
TYPE min_; \
TYPE max_; \
}NAME##_prop(&NAME); \
main.cpp
main
{
uint32 u(255);
META_PROPERTY(u, uint32, ACCESS_DEFAULT, 0, 1024);
...
}
尽管构造函数不完整,宏还是愉快地创建了 NAME##_MetaProperty 对象,我想我理解为什么预处理器只是填写 MIN 和 MAX 参数。但是,如果我将构造函数更改为以下内容,则会出现一堆编译错误。
public: \
NAME##_MetaProperty(TYPE *NAME, TYPE MIN, TYPE MAX) \
: GetSet(NAME, ACCESS) \
, min_(MIN) \
, max_(MAX) \
{} \
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2143: syntax error: missing ')' before 'constant'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2143: syntax error: missing ';' before 'constant'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2059: syntax error: 'constant'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2059: syntax error: ')'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2334: unexpected token(s) preceding ':'; skipping apparent function body
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2059: syntax error: '&'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): warning C4183: 'u_prop': missing return type; assumed to be a member function returning 'int'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(45): error C2059: syntax error: '='
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(45): error C2238: unexpected token(s) preceding ';'
如果我预定义常量,我会得到另一组更小的错误。
main
{
const uint32 min(0);
const uint32 max(1024);
META_PROPERTY(u, uint32, ACCESS_DEFAULT, min, max);
}
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2664: 'main::u_MetaProperty::u_MetaProperty(const main::u_MetaProperty &)': cannot convert argument 1 from 'uint32 *' to 'const main::u_MetaProperty &'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): note: Reason: cannot convert from 'uint32 *' to 'const main::u_MetaProperty'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): note: No constructor could take the source type, or constructor overload resolution was ambiguous
我只是想了解为什么我不能通过构造函数传递 MIN 和 MAX。这个宏对我的常量做了什么?
您更改 c'tor 的方式使其扩展为
u_MetaProperty(uint32 *u, uint32 0, uint32 1024)
这些是无效的标识符。
如果您真的坚持将 MAX 和 MIN 作为构造函数参数传递,那么您可以这样做:
#define META_PROPERTY(NAME, TYPE, ACCESS, MIN, MAX) \
class NAME##_MetaProperty : public sge::GetSet<TYPE> \
{ \
public: \
NAME##_MetaProperty(TYPE *NAME, TYPE min, TYPE max) \
: GetSet(NAME, ACCESS) \
, min_(min) \
, max_(max) \
{} \
. . . other methods . . .
private: \
TYPE min_; \
TYPE max_; \
}NAME##_prop(&NAME, MIN, MAX); \
但是由于它们应该是编译时常量,宏已经很好地完成了它的工作。