- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个宏来为每个参数调用静态函数。
例如:
#define FOO(X) X::do();
#define FOO_1(X,Y) X::do(); Y::do();
我的问题是我需要使用参数数量可变的 foo ,是否可以使用 __VA_ARGS__
?
喜欢下面这行:
#define FOO(...) __VA_ARGS__::do() ?
谢谢
最佳答案
宏扩展不像使用可变参数模板的参数包扩展那样工作。您所拥有的将扩展为:
X,Y::do();
而不是
X::do(); Y::do();
如你所愿。但在 C++11 中,您可以使用可变参数模板。例如,您可以通过这种方式执行您想要的操作:
#include <iostream>
struct X { static void foo() { std::cout << "X::foo()" << std::endl; }; };
struct Y { static void foo() { std::cout << "Y::foo()" << std::endl; }; };
struct Z { static void foo() { std::cout << "Z::foo()" << std::endl; }; };
int main()
{
do_foo<X, Y, Z>();
}
您只需要这个(相对简单的)机器:
namespace detail
{
template<typename... Ts>
struct do_foo;
template<typename T, typename... Ts>
struct do_foo<T, Ts...>
{
static void call()
{
T::foo();
do_foo<Ts...>::call();
}
};
template<typename T>
struct do_foo<T>
{
static void call()
{
T::foo();
}
};
}
template<typename... Ts>
void do_foo()
{
detail::do_foo<Ts...>::call();
}
这是一个live example .
关于c++ - 如何提取__VA_ARGS__?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16372780/
我有以下两个宏: #define F1(...) [NSString stringWithFormat:__VA_ARGS__] #define F2(format, ...) [NSString s
我想知道##在这个宏定义中做了什么: #define debug(M, ...) fprintf(stderr,M "\n",##__VA_ARGS __) 我在谷歌上搜索了答案,并得出了以下结果。
C++ preprocessor __VA_ARGS__ number of arguments 那里接受的答案对我不起作用。我试过 MSVC++ 10 和 g++ 3.4.5。 我还将示例压缩成更小
此代码片段是否可移植? 定义: #define log(...) std::cout << __FILE__ << "[" << __LINE__ << "]:" \
在可变参数宏中,#__VA_ARGS__是所有参数的逗号分隔字符串(至少我用 gcc 和 clang 得到了这种行为)。有没有一种方法可以在不解析该字符串的情况下为各个参数创建一个字符串数组? 我正在
我有以下宏: #define MY_FCT1( id, ... ) \ FCT( id,__VA_ARGS__ ); \ 我想创建一个新的来做这样的事情: #de
variadic macro提到了 gcc 的 VA_ARGS。 我做了以下实验。 #define EVAL(f,...) eval(f,build_args(args,__VA_ARGS__ , a
我知道我可以做到: #define MACRO(api, ...) \ bool ret = api(123, ##__VA_ARGS__); 这只是一个示例,它是更复杂解决方案的一部分。关键是我
我正在尝试为数组数据冗余编写一个特殊的类型处理。这个想法是在编译时全局定义和声明一个固定大小的数组,但每个声明的数组的大小都不同。这是想法: array[N] = { el1, el2, el3, .
我正在尝试制作一个类似于此的函数: #define printf_copy(s, ...) printf(s, ##__VA_ARGS__) // acceptable! 但这是一个预处理器,我需
如果我尝试编译以下代码: template void Dummy(const TArgs &...args) { } #define DUMMY(...) Dummy("Hello", ##__VA
我想定义一个宏,例如 - #define log(lognumber,...) logreport(lognumber,__VA_ARGS__) 我想检查 __VA_ARGS__ 用户调用 log
假设我们有以下两个函数: void foo1(int p); void foo2(int p, ...); 我想编写一个宏,根据参数的数量自动扩展为正确的宏。我使用了以下肮脏/黑客的方式,但我很好奇是
假设我有一个宏: #define FOO(a, ...) if (a) foo(a, ## __VA_ARGS__) 这很好用: FOO(a)将转换为 if (a) foo(a) FOO(a, )将转
只是为了了解一点背景知识,这不是一个微不足道的练习!我正在使用 Boost.Python,为了避免大量丑陋的样板代码,我使用宏将函数包装在 Python 包装器类中,以选择性地为该方法调用 Pytho
我正在尝试重新定义可变参数宏以使用 cout 而不是 printf。这是原始代码: #define LOGE(...) PRINT_LEVEL(1, __VA_ARGS__); #define P
我正在尝试将 __ VA_ARGS __ 传递给一个函数。由于某种原因,第一个参数总是错误的(看起来像一个地址): #define PRINTF_ERROR(format, ...) {\
我已经从互联网上下载并重新编译了一个库,结果是由于非法指针地址导致核心转储。 查看源码,涉及的语句有: #define E_DEBUG(level, ...) \ if (err_get_de
我正在开发一个嵌入式应用程序,它使用一个库来连接 SPI NAND 内存芯片。提供的库使用打印功能,作为应用程序开发人员,我必须针对我的特定平台覆盖该功能。 例如,他们在代码中广泛使用函数 print
我正在为 SQL 代码使用预处理器宏 va_args hack,以允许直接粘贴到 sqlite3.exe 中以进行快速的非构建调试: #define QUOTE(...) #__VA_ARGS__ c
我是一名优秀的程序员,十分优秀!