在c ++中,是否有一种方法可以在扩展宏时评估简单的算术运算? (类似于(a + b)/ c)
我需要使用操作结果将其连接为令牌并生成唯一的函数名称
示例:
#define MAKE_MACRO2(x, y) x##y
#define MAKE_MACRO(x, y) MAKE_MACRO2(x, y)
#define MAKE_COMPUTE(x) x
#define MAKE_INITIAL_FUNC() static bool MAKE_MACRO(_Construct, __COUNTER__)();
#define CALL_NEXT_FUNC() return MAKE_MACRO(_Construct, __COUNTER__ + 1 - 2)();
#define MAKE_NEXT_FUNC() static bool MAKE_MACRO(_Construct, __COUNTER__ + 1)();
#define START_NEXT_FUNC() static bool MAKE_MACRO(_Construct, __COUNTER__ - 3)()
#define START_CLASS(name) \
class name \
{ \
private: \
static bool m_sbConstructed;\
protected: \
MAKE_INITIAL_FUNC() \
static bool Construct() \
{ \
if (!m_sbConstructed) \
{
#define ADD_CALL(name, text) \
CALL_NEXT_FUNC() \
} \
return TRUE; \
} \
MAKE_NEXT_FUNC() \
public: \
void Print##name() \
{ \
printf("%s\n", text); \
} \
protected: \
START_NEXT_FUNC() \
{ \
{
#define END_CLASS() \
} \
m_sbConstructed = TRUE; \
return TRUE; \
} \
};
#define DEF_CLASS(name) \
bool name::m_sbConstructed = FALSE;
START_CLASS(classe1)
ADD_CALL(alpha, "salut")
ADD_CALL(bravo, "salut2")
ADD_CALL(charlie, "hey ho")
ADD_CALL(delta, "ta gueule")
END_CLASS()
DEF_CLASS(classe1)
将扩展为:
class classe1
{
private:
static bool m_sbConstructed;
protected:
static bool _Construct0();
static bool Construct()
{
if (!m_sbConstructed)
{
return _Construct0();
}
return 1;
}
static bool _Construct3();
public:
void Printalpha()
{
printf("%s\n", "salut");
}
protected:
static bool _Construct0()
{
{
return _Construct3();
}
return 1;
}
static bool _Construct6();
public:
void Printbravo()
{
printf("%s\n", "salut2");
}
protected:
static bool _Construct3()
{
{
return _Construct6();
}
return 1;
}
static bool _Construct9();
public:
void Printcharlie()
{
printf("%s\n", "hey ho");
}
protected:
static bool _Construct6()
{
{
return _Construct9();
}
return 1;
}
static bool _Construct12();
public:
void Printdelta()
{
printf("%s\n", "ta gueule");
}
protected:
static bool _Construct9()
{
{
}
m_sbConstructed = 1;
return 1;
}
};
bool classe1::m_sbConstructed = 0;
代替:
class classe1
{
private:
static bool m_sbConstructed;
protected:
static bool _Construct0();
static bool Construct()
{
if (!m_sbConstructed)
{
return _Construct1 + 1 - 2();
}
return 1;
}
static bool _Construct2 + 1();
public:
void Printalpha()
{
printf("%s\n", "salut");
}
protected:
static bool _Construct3 - 3()
{
{
return _Construct4 + 1 - 2();
}
return 1;
}
static bool _Construct5 + 1();
public:
void Printbravo()
{
printf("%s\n", "salut2");
}
protected:
static bool _Construct6 - 3()
{
{
return _Construct7 + 1 - 2();
}
return 1;
}
static bool _Construct8 + 1();
public:
void Printcharlie()
{
printf("%s\n", "hey ho");
}
protected:
static bool _Construct9 - 3()
{
{
return _Construct10 + 1 - 2();
}
return 1;
}
static bool _Construct11 + 1();
public:
void Printdelta()
{
printf("%s\n", "ta gueule");
}
protected:
static bool _Construct12 - 3()
{
{
}
m_sbConstructed = 1;
return 1;
}
};
bool classe1::m_sbConstructed = 0;
如果我还记得上课的话,预处理器不是做这种扩展的,但是也许我错过了什么?
目标是使用宏生成唯一的函数名称,但仍然使用宏对其进行调用和定义