宏重载 [英] Macro overloading

查看:76
本文介绍了宏重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以定义如下内容:

Is it possible to define something like this:

#define FOO(x, y) BAR()
#define FOO(x, sth, y) BAR(sth)

这样:

FOO("daf", sfdas);
FOO("fdsfs", something, 5);

被翻译为:

BAR();
BAR(something);

?

实际上,BAR是我班上的方法.抱歉,您以前没有这么说过(没想到这很重要).

Actually, BAR's are methods of my class. Sorry for not saying that before (didn't think it was relevant).

回答DyP的问题:

class Testy
{
    public:
    void TestFunction(std::string one, std::string two, std::string three)
    {
        std::cout << one << two << three;
    }
    void AnotherOne(std::string one)
    {
        std::cout << one;
    }
    void AnotherOne(void)
    {
        std::cout << "";
    }
};

#define PP_NARG(...) PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
#define PP_NARG_(...) PP_ARG_N(__VA_ARGS__)
#define PP_ARG_N(_1, _2, _3, N, ...) N
#define PP_RSEQ_N() 3, 2, 1, 0

// macro for exactly 2 arguments
#define FOO_2(_1, _2) AnotherOne()
// macro for exactly 3 arguments
#define FOO_3(_1, _2, _3) AnotherOne(_2)

// macro selection by number of arguments
#define FOO_(N) FOO_##N
#define FOO_EVAL(N) FOO_(N)
#define TestFunction(...) FOO_EVAL(PP_NARG(__VA_ARGS__))(__VA_ARGS__)

然后致电:

Testy testy;
testy.TestFunction("one", "two", "three");   // line 9

编译器输出:

警告1警告C4003:宏'PP_ARG_N'main.cpp 9的实际参数不足

Warning 1 warning C4003: not enough actual parameters for macro 'PP_ARG_N' main.cpp 9

警告2警告C4003:宏'FOO_'main.cpp 9的实际参数不足

Warning 2 warning C4003: not enough actual parameters for macro 'FOO_' main.cpp 9

错误3错误C2039:'FOO_':不是'Testy'main.cpp 9的成员

Error 3 error C2039: 'FOO_' : is not a member of 'Testy' main.cpp 9

推荐答案

编辑-------------------------------- - - - - - - - - 看这里 - - - - - - - - - - - - - - - - --------------------------------->

Edit ------------------------------------------------look here----------------------------------------------------------------->

(根据参数数量重载宏)

// functions, or macros, ....
void bar(){}
void bar(int){}

#define EXPAND(X) X    // for MSVC10 compatibility

// compute number of (variadic) macro arguments
// from http://groups.google.com/group/comp.std.c/browse_thread/thread/77ee8c8f92e4a3fb/346fc464319b1ee5?pli=1
#define PP_NARG(...) EXPAND( PP_NARG_(__VA_ARGS__, PP_RSEQ_N()) )
#define PP_NARG_(...) EXPAND( PP_ARG_N(__VA_ARGS__) )
#define PP_ARG_N(_1, _2, _3, N, ...) N
#define PP_RSEQ_N() 3, 2, 1, 0


// macro for exactly 2 arguments
#define FOO_2(_1, _2) bar()
// macro for exactly 3 arguments
#define FOO_3(_1, _2, _3) bar(_2)

// macro selection by number of arguments
#define FOO_(N) FOO_##N
#define FOO_EVAL(N) FOO_(N)
#define FOO(...) EXPAND( FOO_EVAL(EXPAND( PP_NARG(__VA_ARGS__) ))(__VA_ARGS__) )

int main()
{
    int something = 42;
    FOO("daf", sfdas);
    FOO("fdsfs", something, 5);
}

预处理器输出:

void bar(){}
void bar(int){}

int main()
{
    int something = 42;
    bar();
    bar(something);
}


Edit2:似乎VS2010在__VA_ARGS__和宏替换方面存在一些问题.


Seems like VS2010 has some issues with __VA_ARGS__ and macro replacement.

更新:这是一个错误? (另请参见此SO问题):

UPDATE: It's ... a bug?? (also see this SO question):

#define MACRO2(PARAM0, PARAM1, ...) arg 0: >PARAM0<    arg 1: >PARAM1<    \
  additional args: >__VA_ARGS__<
#define MACRO1(...) MACRO2(__VA_ARGS__, OTHERARG_0, OTHERARG_1)

MACRO1(ARG0, ARG1);

预处理器输出:

arg 0: >ARG0, ARG1<    arg 1: >OTHERARG_0<    additional args: >OTHERARG_1<;

有关解决方法,请参阅链接的SO问题.我已经更新了上面的原始答案(代码),并使用MSVC10->现在对其进行了测试.

For a workaround, see the linked SO question. I've updated the original answer (code) above and tested it with MSVC10 -> works now.

这篇关于宏重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆