简体   繁体   English

了解带有常量的预处理器宏

[英]Understanding preprocessor macros with constants

I am experimenting with C++ reflection methods and having problems understanding preprocessor macros. 我正在尝试使用C ++反射方法,并且在理解预处理器宏时遇到问题。 For example, the following code works. 例如,以下代码有效。

header.h: 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.cpp中

main
{
    uint32 u(255);
    META_PROPERTY(u, uint32, ACCESS_DEFAULT, 0, 1024);
    ...
}

The macro happily creates the NAME##_MetaProperty object despite the incomplete constructor and I guess I understand why since the preprocessor simply fills in the MIN an MAX parameters. 尽管构造函数不完整,但该宏还是很高兴地创建了NAME ## _ MetaProperty对象,而且我想我能理解为什么因为预处理器只是简单地填写MIN和MAX参数。 However, if I change the constructor to the following I get a bunch of compile errors. 但是,如果将构造函数更改为以下内容,则会出现一堆编译错误。

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 ';'

If I predefine the constants, I get another, smaller set of errors. 如果我预定义常量,则会得到另一组较小的错误。

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

I am just trying to understand why I cannot pass MIN and MAX through the constructor. 我只是想了解为什么我不能通过构造函数传递MIN和MAX。 What is this macro doing with my constants? 这个宏对我的常量做什么?

The way you changed the c'tor makes it expand to 您更改c'tor的方式使其扩展到

u_MetaProperty(uint32 *u, uint32 0, uint32 1024)

Those are invalid identifiers. 这些是无效的标识符。

If you are truly adamant on passing MAX and MIN as constructor parameters, this is how you'd do it: 如果您确实坚持将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); \

But since they are supposed to be compile time constants, the macro does its job well already. 但是,由于它们应该是编译时间常数,因此宏已经做好了工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM