C++ Macro: Number of Arguments | SF-Zhou's Blog #171
SF-Zhou
announced in
Announcements
Replies: 2 comments
-
|
宏的应用还是很丰富的,比如可能会遇到的函数流程控制,可能需要下面这种结构: do {
...;
if(...) break;
...;
if (...) break;
...;
} while(0);可以使用宏搭配一个类解决这个问题,甚至还增加了返回值的能力: class A {
public:
template <class F>
auto operator-(const F &func) -> typename std::result_of<F()>::type {
return func();
}
};
#define BLOCK A() - [&]
int ret = BLOCK {
...;
if (...) return -1;
...;
if (...) return -2;
...;
return 0;
}; |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
C++ 20 引入了 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
https://sf-zhou.github.io/programming/cpp_macro_number_of_arguments.html
Beta Was this translation helpful? Give feedback.
All reactions