如何使用 C++ 98 标准轻松创建完整的 "variadic" 函数?
How to easily create fully "variadic" functions with C++ 98 standard?
图书馆
https://github.com/c42f/tinyformat/blob/2f9335afd9941688e42d60cae5166b9f0600b2d1/tinyformat.h#L1104-L1116,使用这个很棒的技巧在 C++ 98 上做 "variadic" 模板:
inline void printfln(const char* fmt)
{
format(std::cout, fmt);
std::cout << '\n';
}
template<TINYFORMAT_ARGTYPES(n)> \
void printfln(const char* fmt, TINYFORMAT_VARARGS(n)) \
{ \
format(std::cout, fmt, TINYFORMAT_PASSARGS(n)); \
std::cout << '\n'; \
}
我正在尝试通过消除将函数 printfln
复制两次的要求来改进它,即基本情况一次 inline void printfln(const char* fmt)
,第二次 "variadic" 部分 template<TINYFORMAT_ARGTYPES(n)> void printfln(const char* fmt, TINYFORMAT_VARARGS(n))
.
他们需要将printfln
函数分成两部分,因为"variadic"函数只能接受一个参数,即printfln("something")
。在这种情况下,TINYFORMAT_VARARGS(n)
必须展开为空,但是,这样做会导致代码尾随逗号 ,
,导致 C++ 语法无效。
我可以使用 GNU GCC
扩展技巧和 C 宏标记粘贴运算符 ##
来删除但是,尾随逗号是不可移植的,因为它仅适用于 GNU GCC
。然后,我的目标是定义已经包含前导逗号的宏作为下一个示例:
#include <stdio.h>
#define TINYFORMAT_ARGTYPES_0
#define TINYFORMAT_ARGTYPES_1 , class T1
#define TINYFORMAT_ARGTYPES_2 , class T1, class T2
#define TINYFORMAT_ARGTYPES_3 , class T1, class T2, class T3
#define TINYFORMAT_VARARGS_0
#define TINYFORMAT_VARARGS_1 , const T1& v1
#define TINYFORMAT_VARARGS_2 , const T1& v1, const T2& v2
#define TINYFORMAT_VARARGS_3 , const T1& v1, const T2& v2, const T3& v3
#define TINYFORMAT_PASSARGS_0
#define TINYFORMAT_PASSARGS_1 , v1
#define TINYFORMAT_PASSARGS_2 , v1, v2
#define TINYFORMAT_PASSARGS_3 , v1, v2, v3
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
#define FACTORY(n) \
template<typename T0 TINYFORMAT_ARGTYPES(n)> \
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
{ \
fprintf(stderr, v0 TINYFORMAT_PASSARGS(n) ); \
}
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
int main(int argc, char const *argv[])
{
some( "Something %s.", "New" );
}
但是,它不起作用。 gcc
编译器在 if 发现 comma
时发疯,就在宏定义之后:g++ -o main -g -ggdb test_debugger.cpp --std=c++98 && ./main
test_debugger.cpp:21:19: error: expected nested-name-specifier before ‘T0’
template<typename T0 TINYFORMAT_ARGTYPES(n)> \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:21:22: error: expected ‘>’ before ‘TINYFORMAT_ARGTYPES’
template<typename T0 TINYFORMAT_ARGTYPES(n)> \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:24: error: ‘T0’ does not name a type
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:31: error: expected ‘,’ or ‘...’ before ‘TINYFORMAT_VARARGS’
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:52: error: default template arguments may not be used in function templates without -std=c++11 or -std=gnu++11
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp: In function ‘void some(const int&)’:
test_debugger.cpp:24:24: error: expected ‘)’ before ‘TINYFORMAT_PASSARGS’
fprintf(stderr, v0 TINYFORMAT_PASSARGS(n) ); \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp: At global scope:
test_debugger.cpp:21:19: error: expected nested-name-specifier before ‘T0’
template<typename T0 TINYFORMAT_ARGTYPES(n)> \
^
test_debugger.cpp:18:43: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:21:22: error: expected ‘>’ before ‘TINYFORMAT_ARGTYPES’
template<typename T0 TINYFORMAT_ARGTYPES(n)> \
^
test_debugger.cpp:18:43: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:24: error: ‘T0’ does not name a type
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:43: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:31: error: expected ‘,’ or ‘...’ before ‘TINYFORMAT_VARARGS’
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:43: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:52: error: default template arguments may not be used in function templates without -std=c++11 or -std=gnu++11
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:43: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:13: error: redefinition of ‘template<<declaration error> > void some(const int&)’
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:43: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:13: note: ‘template<<declaration error> > void some(const int&)’ previously declared here
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:21:19: error: expected nested-name-specifier before ‘T0’
template<typename T0 TINYFORMAT_ARGTYPES(n)> \
^
test_debugger.cpp:18:48: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:21:22: error: expected ‘>’ before ‘TINYFORMAT_ARGTYPES’
template<typename T0 TINYFORMAT_ARGTYPES(n)> \
^
test_debugger.cpp:18:48: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:24: error: ‘T0’ does not name a type
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:48: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:31: error: expected ‘,’ or ‘...’ before ‘TINYFORMAT_VARARGS’
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:48: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:52: error: default template arguments may not be used in function templates without -std=c++11 or -std=gnu++11
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:48: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:13: error: redefinition of ‘template<<declaration error> > void some(const int&)’
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:48: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:13: note: ‘template<<declaration error> > void some(const int&)’ previously declared here
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:21:19: error: expected nested-name-specifier before ‘T0’
template<typename T0 TINYFORMAT_ARGTYPES(n)> \
^
test_debugger.cpp:18:53: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:21:22: error: expected ‘>’ before ‘TINYFORMAT_ARGTYPES’
template<typename T0 TINYFORMAT_ARGTYPES(n)> \
^
test_debugger.cpp:18:53: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:24: error: ‘T0’ does not name a type
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:53: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:31: error: expected ‘,’ or ‘...’ before ‘TINYFORMAT_VARARGS’
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:53: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:52: error: default template arguments may not be used in function templates without -std=c++11 or -std=gnu++11
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:53: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:13: error: redefinition of ‘template<<declaration error> > void some(const int&)’
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:53: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:13: note: ‘template<<declaration error> > void some(const int&)’ previously declared here
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp: In function ‘int main(int, const char**)’:
test_debugger.cpp:31:34: error: no matching function for call to ‘some(const char [14], const char [4])’
some( "Something %s.", "New" );
^
test_debugger.cpp:22:13: note: candidate: template<<declaration error> > void some(const int&)
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:13: note: template argument deduction/substitution failed:
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:31:34: note: candidate expects 1 argument, 2 provided
some( "Something %s.", "New" );
^
据我所知,我的 TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
宏应该创建以下 4 个有效的 C++ "variadic" 函数:
template<typename T0>
inline void some(const T0& v0)
{
fprintf(stderr, v0 );
}
template<typename T0, class T1>
inline void some(const T0& v0, const T1& v1)
{
fprintf(stderr, v0, v1 );
}
template<typename T0, class T1, class T2>
inline void some(const T0& v0, const T1& v1, const T1& v2)
{
fprintf(stderr, v0, v1, v2);
}
template<typename T0, class T1, class T2, class T3>
inline void some(const T0& v0, const T1& v1, const T1& v2, const T1& v3)
{
fprintf(stderr, v0, v1, v2, v3);
}
为什么 gcc
预处理器不能正确生成我上面的 4 "variadic" 模板函数?
作为参考,我正在使用:
$ g++ --version
g++ (GCC) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
更新
g++ -o main -E -g -ggdb test_debugger.cpp --std=c++98
的输出
# 797 "/usr/include/stdio.h" 3 4
}
# 2 "test_debugger.cpp" 2
# 27 "test_debugger.cpp"
# 27 "test_debugger.cpp"
template<typename T0 TINYFORMAT_ARGTYPES(0)>
inline void some(const T0& v0 TINYFORMAT_VARARGS(0)) { fprintf(
# 27 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 27 "test_debugger.cpp"
, v0 TINYFORMAT_PASSARGS(0) ); } template<typename T0 TINYFORMAT_ARGTYPES(1)>
inline void some(const T0& v0 TINYFORMAT_VARARGS(1)) { fprintf(
# 27 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 27 "test_debugger.cpp"
, v0 TINYFORMAT_PASSARGS(1) ); } template<typename T0 TINYFORMAT_ARGTYPES(2)>
inline void some(const T0& v0 TINYFORMAT_VARARGS(2)) { fprintf(
# 27 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 27 "test_debugger.cpp"
, v0 TINYFORMAT_PASSARGS(2) ); } template<typename T0 TINYFORMAT_ARGTYPES(3)>
inline void some(const T0& v0 TINYFORMAT_VARARGS(3)) { fprintf(
# 27 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 27 "test_debugger.cpp"
, v0 TINYFORMAT_PASSARGS(3) ); }
int main(int argc, char const *argv[])
{
some( "Something %s.", "New" );
}
更新 2
我试图改变定义的顺序,希望 C 预处理器能正确地扩展,但它只是像以前一样扩展:
#include <stdio.h>
#define TINYFORMAT_ARGTYPES_0
#define TINYFORMAT_ARGTYPES_1 , class T1
#define TINYFORMAT_ARGTYPES_2 , class T1, class T2
#define TINYFORMAT_ARGTYPES_3 , class T1, class T2, class T3
#define TINYFORMAT_VARARGS_0
#define TINYFORMAT_VARARGS_1 , const T1& v1
#define TINYFORMAT_VARARGS_2 , const T1& v1, const T2& v2
#define TINYFORMAT_VARARGS_3 , const T1& v1, const T2& v2, const T3& v3
#define TINYFORMAT_PASSARGS_0
#define TINYFORMAT_PASSARGS_1 , v1
#define TINYFORMAT_PASSARGS_2 , v1, v2
#define TINYFORMAT_PASSARGS_3 , v1, v2, v3
#define TINYFORMAT_FOREACH_ARGNUM(m) \
m(TINYFORMAT_ARGTYPES(0),TINYFORMAT_VARARGS(0),TINYFORMAT_PASSARGS(0)) \
m(TINYFORMAT_ARGTYPES(1),TINYFORMAT_VARARGS(1),TINYFORMAT_PASSARGS(1)) \
m(TINYFORMAT_ARGTYPES(2),TINYFORMAT_VARARGS(2),TINYFORMAT_PASSARGS(2)) \
m(TINYFORMAT_ARGTYPES(3),TINYFORMAT_VARARGS(3),TINYFORMAT_PASSARGS(3))
#define FACTORY(argtypes,varargs,passargs) \
template<typename T0 argtypes> \
inline void some(const T0& v0 varargs) \
{ \
fprintf(stderr, v0 passargs); \
}
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
int main(int argc, char const *argv[])
{
some( "Something %s.", "New" );
}
更新 3
正如@aschepler所说,我遗漏了TINYFORMAT_ARGTYPES_ ## n
的定义,这是一个固定版本:
#include <stdio.h>
#define TINYFORMAT_ARGTYPES_0(...)
#define TINYFORMAT_ARGTYPES_1(...) __VA_ARGS__ class T1
#define TINYFORMAT_ARGTYPES_2(...) __VA_ARGS__ class T1, class T2
#define TINYFORMAT_ARGTYPES_3(...) __VA_ARGS__ class T1, class T2, class T3
#define TINYFORMAT_VARARGS_0(...)
#define TINYFORMAT_VARARGS_1(...) __VA_ARGS__ const T1& v1
#define TINYFORMAT_VARARGS_2(...) __VA_ARGS__ const T1& v1, const T2& v2
#define TINYFORMAT_VARARGS_3(...) __VA_ARGS__ const T1& v1, const T2& v2, const T3& v3
#define TINYFORMAT_PASSARGS_0(...)
#define TINYFORMAT_PASSARGS_1(...) __VA_ARGS__ v1
#define TINYFORMAT_PASSARGS_2(...) __VA_ARGS__ v1, v2
#define TINYFORMAT_PASSARGS_3(...) __VA_ARGS__ v1, v2, v3
#define TINYFORMAT_ARGTYPES(n,...) TINYFORMAT_ARGTYPES_ ## n (__VA_ARGS__)
#define TINYFORMAT_VARARGS(n,...) TINYFORMAT_VARARGS_ ## n (__VA_ARGS__)
#define TINYFORMAT_PASSARGS(n,...) TINYFORMAT_PASSARGS_ ## n (__VA_ARGS__)
#define TINYFORMAT_FOREACH_ARGNUM(m,...) \
m(0) m(1,__VA_ARGS__) m(2,__VA_ARGS__) m(3,__VA_ARGS__)
#define FACTORY(n,...) \
template<TINYFORMAT_ARGTYPES(n,__VA_ARGS__)> \
inline void some(TINYFORMAT_VARARGS(n,__VA_ARGS__)) \
{ \
fprintf(stderr, "variadic" TINYFORMAT_PASSARGS(n,,) ); \
}
TINYFORMAT_FOREACH_ARGNUM(FACTORY,)
int main(int argc, char const *argv[]) {
some();
}
扩展到:g++ -o main -E -g -ggdb test_debugger.cpp --std=c++98
# 797 "/usr/include/stdio.h" 3 4
}
# 2 "test_debugger.cpp" 2
# 31 "test_debugger.cpp"
# 31 "test_debugger.cpp"
template<>
inline void some() { fprintf(
# 31 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 31 "test_debugger.cpp"
, "variadic" ); } template< class T1>
inline void some( const T1& v1) { fprintf(
# 31 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 31 "test_debugger.cpp"
, "variadic" , v1 ); } template< class T1, class T2>
inline void some( const T1& v1, const T2& v2) { fprintf(
# 31 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 31 "test_debugger.cpp"
, "variadic" , v1, v2 ); } template< class T1, class T2, class T3>
inline void some( const T1& v1, const T2& v2, const T3& v3) { fprintf(
# 31 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 31 "test_debugger.cpp"
, "variadic" , v1, v2, v3 ); }
int main(int argc, char const *argv[]) {
some();
}
现在的问题是当宏模板扩展为没有任何模板参数的东西时,即 template<> inline void some()
,它会生成以下代码:
template<>
inline void some()
{
fprintf(stderr, "variadic");
}
int main(int argc, char const *argv[])
{
some();
}
这导致 C++ 编译器抛出此错误:
test_debugger.cpp: error: ‘some’ is not a template function
inline void some()
^
test_debugger.cpp: In function ‘int main(int, const char**)’:
test_debugger.cpp: error: ‘some’ was not declared in this scope
some();
^~~~
模板不能有零个模板参数。以 template <>
开头的语法改为用于显式特化:要使用的声明,而不是用于一组特定模板参数的模板。
因此您的零参数版本需要跳过 template <>
部分。你可以这样做:
#define TINYFORMAT_TEMPLATE_HEAD_0(...)
#define TINYFORMAT_TEMPLATE_HEAD_1(...) template < TINYFORMAT_ARGTYPES_1(__VA_ARGS__) >
#define TINYFORMAT_TEMPLATE_HEAD_2(...) template < TINYFORAMT_ARGTYPES_2(__VA_ARGS__) >
#define TINYFORMAT_TEMPLATE_HEAD_3(...) template < TINYFORMAT_ARGTYPES_3(__VA_ARGS__) >
#define TINYFORMAT_TEMPLATE_HEAD(n, ...) TINYFORMAT_TEMPLATE_HEAD_ ## n (__VA_ARGS__)
#define FACTORY(n,...) \
TINYFORMAT_TEMPLATE_HEAD(n,__VA_ARGS__) \
inline void some(TINYFORMAT_VARARGS(n,__VA_ARGS__)) \
{ \
fprintf(stderr, "variadic" TINYFORMAT_PASSARGS(n,,) ); \
}
感谢 @aschepler 的帮助,我也成功地构建了这个更通用的 solution/example:
#include <stdio.h>
#define TINYFORMAT_ARGTYPES_0(...)
#define TINYFORMAT_ARGTYPES_1(begin,end,...) begin __VA_ARGS__ class T1 end
#define TINYFORMAT_ARGTYPES_2(begin,end,...) begin __VA_ARGS__ class T1, class T2 end
#define TINYFORMAT_ARGTYPES_3(begin,end,...) begin __VA_ARGS__ class T1, class T2, class T3 end
#define TINYFORMAT_VARARGS_0(...)
#define TINYFORMAT_VARARGS_1(...) __VA_ARGS__ const T1& v1
#define TINYFORMAT_VARARGS_2(...) __VA_ARGS__ const T1& v1, const T2& v2
#define TINYFORMAT_VARARGS_3(...) __VA_ARGS__ const T1& v1, const T2& v2, const T3& v3
#define TINYFORMAT_PASSARGS_0(...)
#define TINYFORMAT_PASSARGS_1(...) __VA_ARGS__ v1
#define TINYFORMAT_PASSARGS_2(...) __VA_ARGS__ v1, v2
#define TINYFORMAT_PASSARGS_3(...) __VA_ARGS__ v1, v2, v3
#define TINYFORMAT_ARGTYPES(n,begin,end,...) TINYFORMAT_ARGTYPES_ ## n (begin,end,__VA_ARGS__)
#define TINYFORMAT_VARARGS(n,...) TINYFORMAT_VARARGS_ ## n (__VA_ARGS__)
#define TINYFORMAT_PASSARGS(n,...) TINYFORMAT_PASSARGS_ ## n (__VA_ARGS__)
#define TINYFORMAT_FOREACH_ARGNUM(m,...) \
m(0) m(1,__VA_ARGS__) m(2,__VA_ARGS__) m(3,__VA_ARGS__)
#define FACTORY_FULLY_OPTIONAL(n,...) \
TINYFORMAT_ARGTYPES(n,template<,>,__VA_ARGS__) \
inline void some_optional(TINYFORMAT_VARARGS(n,__VA_ARGS__)) \
{ \
fprintf( stderr, "some_optional\n" TINYFORMAT_PASSARGS(n,,) ); \
}
#define FACTORY_WITH_EXISTENT_ARGS(n,...) \
template<class T0 TINYFORMAT_ARGTYPES(n,,,__VA_ARGS__)> \
inline void some_existent(const T0& v0 TINYFORMAT_VARARGS(n,__VA_ARGS__)) \
{ \
fprintf( stderr, "some_existent %s\n", v0 TINYFORMAT_PASSARGS(n,__VA_ARGS__) ); \
}
TINYFORMAT_FOREACH_ARGNUM(FACTORY_FULLY_OPTIONAL,)
TINYFORMAT_FOREACH_ARGNUM(FACTORY_WITH_EXISTENT_ARGS,,)
int main(int argc, char const *argv[])
{
some_optional();
some_existent( "varing" );
}
这是生成的代码:g++ -o main -E -g -ggdb test_debugger.cpp --std=c++98
# 797 "/usr/include/stdio.h" 3 4
}
# 2 "test_debugger.cpp" 2
# 38 "test_debugger.cpp"
# 38 "test_debugger.cpp"
inline void some_optional() { fprintf(
# 38 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 38 "test_debugger.cpp"
, "some_optional\n" ); } template< class T1 >
inline void some_optional( const T1& v1) { fprintf(
# 38 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 38 "test_debugger.cpp"
, "some_optional\n" , v1 ); } template< class T1, class T2 >
inline void some_optional( const T1& v1, const T2& v2) { fprintf(
# 38 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 38 "test_debugger.cpp"
, "some_optional\n" , v1, v2 ); } template< class T1, class T2, class T3 >
inline void some_optional( const T1& v1, const T2& v2, const T3& v3) { fprintf(
# 38 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 38 "test_debugger.cpp"
, "some_optional\n" , v1, v2, v3 ); }
template<class T0 >
inline void some_existent(const T0& v0 ) { fprintf(
# 39 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 39 "test_debugger.cpp"
, "some_existent %s\n", v0 ); } template<class T0 , class T1 >
inline void some_existent(const T0& v0 , const T1& v1) { fprintf(
# 39 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 39 "test_debugger.cpp"
, "some_existent %s\n", v0 , v1 ); } template<class T0 , class T1, class T2 >
inline void some_existent(const T0& v0 , const T1& v1, const T2& v2) { fprintf(
# 39 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 39 "test_debugger.cpp"
, "some_existent %s\n", v0 , v1, v2 ); } template<class T0 , class T1, class T2, class T3 >
inline void some_existent(const T0& v0 , const T1& v1, const T2& v2, const T3& v3) { fprintf(
# 39 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 39 "test_debugger.cpp"
, "some_existent %s\n", v0 , v1, v2, v3 ); }
int main(int argc, char const *argv[])
{
some_optional();
some_existent("varing");
}
运行它:./main
some_optional
some_existent varing
相关问题:
- Comma in C/C++ macro
- What are the valid characters for macro names?
- Concatenation of tokens in variadic macros
- Is it possible to iterate over arguments in variadic macros?
- C++ preprocesor macro for accumulating comma-separated strings
- Variadic macros with zero arguments, and commas
- Is it possible to stringify a C macro that contains a comma?
- What type of content is allowed to be used as arguments for C preprocessor macro?
- How to stringify a string which contains a comma?
图书馆 https://github.com/c42f/tinyformat/blob/2f9335afd9941688e42d60cae5166b9f0600b2d1/tinyformat.h#L1104-L1116,使用这个很棒的技巧在 C++ 98 上做 "variadic" 模板:
inline void printfln(const char* fmt)
{
format(std::cout, fmt);
std::cout << '\n';
}
template<TINYFORMAT_ARGTYPES(n)> \
void printfln(const char* fmt, TINYFORMAT_VARARGS(n)) \
{ \
format(std::cout, fmt, TINYFORMAT_PASSARGS(n)); \
std::cout << '\n'; \
}
我正在尝试通过消除将函数 printfln
复制两次的要求来改进它,即基本情况一次 inline void printfln(const char* fmt)
,第二次 "variadic" 部分 template<TINYFORMAT_ARGTYPES(n)> void printfln(const char* fmt, TINYFORMAT_VARARGS(n))
.
他们需要将printfln
函数分成两部分,因为"variadic"函数只能接受一个参数,即printfln("something")
。在这种情况下,TINYFORMAT_VARARGS(n)
必须展开为空,但是,这样做会导致代码尾随逗号 ,
,导致 C++ 语法无效。
我可以使用 GNU GCC
扩展技巧和 C 宏标记粘贴运算符 ##
来删除但是,尾随逗号是不可移植的,因为它仅适用于 GNU GCC
。然后,我的目标是定义已经包含前导逗号的宏作为下一个示例:
#include <stdio.h>
#define TINYFORMAT_ARGTYPES_0
#define TINYFORMAT_ARGTYPES_1 , class T1
#define TINYFORMAT_ARGTYPES_2 , class T1, class T2
#define TINYFORMAT_ARGTYPES_3 , class T1, class T2, class T3
#define TINYFORMAT_VARARGS_0
#define TINYFORMAT_VARARGS_1 , const T1& v1
#define TINYFORMAT_VARARGS_2 , const T1& v1, const T2& v2
#define TINYFORMAT_VARARGS_3 , const T1& v1, const T2& v2, const T3& v3
#define TINYFORMAT_PASSARGS_0
#define TINYFORMAT_PASSARGS_1 , v1
#define TINYFORMAT_PASSARGS_2 , v1, v2
#define TINYFORMAT_PASSARGS_3 , v1, v2, v3
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
#define FACTORY(n) \
template<typename T0 TINYFORMAT_ARGTYPES(n)> \
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
{ \
fprintf(stderr, v0 TINYFORMAT_PASSARGS(n) ); \
}
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
int main(int argc, char const *argv[])
{
some( "Something %s.", "New" );
}
但是,它不起作用。 gcc
编译器在 if 发现 comma
时发疯,就在宏定义之后:g++ -o main -g -ggdb test_debugger.cpp --std=c++98 && ./main
test_debugger.cpp:21:19: error: expected nested-name-specifier before ‘T0’
template<typename T0 TINYFORMAT_ARGTYPES(n)> \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:21:22: error: expected ‘>’ before ‘TINYFORMAT_ARGTYPES’
template<typename T0 TINYFORMAT_ARGTYPES(n)> \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:24: error: ‘T0’ does not name a type
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:31: error: expected ‘,’ or ‘...’ before ‘TINYFORMAT_VARARGS’
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:52: error: default template arguments may not be used in function templates without -std=c++11 or -std=gnu++11
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp: In function ‘void some(const int&)’:
test_debugger.cpp:24:24: error: expected ‘)’ before ‘TINYFORMAT_PASSARGS’
fprintf(stderr, v0 TINYFORMAT_PASSARGS(n) ); \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp: At global scope:
test_debugger.cpp:21:19: error: expected nested-name-specifier before ‘T0’
template<typename T0 TINYFORMAT_ARGTYPES(n)> \
^
test_debugger.cpp:18:43: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:21:22: error: expected ‘>’ before ‘TINYFORMAT_ARGTYPES’
template<typename T0 TINYFORMAT_ARGTYPES(n)> \
^
test_debugger.cpp:18:43: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:24: error: ‘T0’ does not name a type
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:43: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:31: error: expected ‘,’ or ‘...’ before ‘TINYFORMAT_VARARGS’
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:43: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:52: error: default template arguments may not be used in function templates without -std=c++11 or -std=gnu++11
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:43: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:13: error: redefinition of ‘template<<declaration error> > void some(const int&)’
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:43: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:13: note: ‘template<<declaration error> > void some(const int&)’ previously declared here
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:21:19: error: expected nested-name-specifier before ‘T0’
template<typename T0 TINYFORMAT_ARGTYPES(n)> \
^
test_debugger.cpp:18:48: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:21:22: error: expected ‘>’ before ‘TINYFORMAT_ARGTYPES’
template<typename T0 TINYFORMAT_ARGTYPES(n)> \
^
test_debugger.cpp:18:48: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:24: error: ‘T0’ does not name a type
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:48: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:31: error: expected ‘,’ or ‘...’ before ‘TINYFORMAT_VARARGS’
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:48: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:52: error: default template arguments may not be used in function templates without -std=c++11 or -std=gnu++11
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:48: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:13: error: redefinition of ‘template<<declaration error> > void some(const int&)’
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:48: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:13: note: ‘template<<declaration error> > void some(const int&)’ previously declared here
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:21:19: error: expected nested-name-specifier before ‘T0’
template<typename T0 TINYFORMAT_ARGTYPES(n)> \
^
test_debugger.cpp:18:53: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:21:22: error: expected ‘>’ before ‘TINYFORMAT_ARGTYPES’
template<typename T0 TINYFORMAT_ARGTYPES(n)> \
^
test_debugger.cpp:18:53: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:24: error: ‘T0’ does not name a type
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:53: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:31: error: expected ‘,’ or ‘...’ before ‘TINYFORMAT_VARARGS’
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:53: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:52: error: default template arguments may not be used in function templates without -std=c++11 or -std=gnu++11
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:53: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:13: error: redefinition of ‘template<<declaration error> > void some(const int&)’
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:53: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:13: note: ‘template<<declaration error> > void some(const int&)’ previously declared here
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp: In function ‘int main(int, const char**)’:
test_debugger.cpp:31:34: error: no matching function for call to ‘some(const char [14], const char [4])’
some( "Something %s.", "New" );
^
test_debugger.cpp:22:13: note: candidate: template<<declaration error> > void some(const int&)
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:22:13: note: template argument deduction/substitution failed:
inline void some(const T0& v0 TINYFORMAT_VARARGS(n)) \
^
test_debugger.cpp:18:38: note: in expansion of macro ‘FACTORY’
#define TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
^
test_debugger.cpp:27:1: note: in expansion of macro ‘TINYFORMAT_FOREACH_ARGNUM’
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
^~~~~~~~~~~~~~~~~~~~~~~~~
test_debugger.cpp:31:34: note: candidate expects 1 argument, 2 provided
some( "Something %s.", "New" );
^
据我所知,我的 TINYFORMAT_FOREACH_ARGNUM(m) m(0) m(1) m(2) m(3)
宏应该创建以下 4 个有效的 C++ "variadic" 函数:
template<typename T0>
inline void some(const T0& v0)
{
fprintf(stderr, v0 );
}
template<typename T0, class T1>
inline void some(const T0& v0, const T1& v1)
{
fprintf(stderr, v0, v1 );
}
template<typename T0, class T1, class T2>
inline void some(const T0& v0, const T1& v1, const T1& v2)
{
fprintf(stderr, v0, v1, v2);
}
template<typename T0, class T1, class T2, class T3>
inline void some(const T0& v0, const T1& v1, const T1& v2, const T1& v3)
{
fprintf(stderr, v0, v1, v2, v3);
}
为什么 gcc
预处理器不能正确生成我上面的 4 "variadic" 模板函数?
作为参考,我正在使用:
$ g++ --version
g++ (GCC) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
更新
g++ -o main -E -g -ggdb test_debugger.cpp --std=c++98
# 797 "/usr/include/stdio.h" 3 4
}
# 2 "test_debugger.cpp" 2
# 27 "test_debugger.cpp"
# 27 "test_debugger.cpp"
template<typename T0 TINYFORMAT_ARGTYPES(0)>
inline void some(const T0& v0 TINYFORMAT_VARARGS(0)) { fprintf(
# 27 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 27 "test_debugger.cpp"
, v0 TINYFORMAT_PASSARGS(0) ); } template<typename T0 TINYFORMAT_ARGTYPES(1)>
inline void some(const T0& v0 TINYFORMAT_VARARGS(1)) { fprintf(
# 27 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 27 "test_debugger.cpp"
, v0 TINYFORMAT_PASSARGS(1) ); } template<typename T0 TINYFORMAT_ARGTYPES(2)>
inline void some(const T0& v0 TINYFORMAT_VARARGS(2)) { fprintf(
# 27 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 27 "test_debugger.cpp"
, v0 TINYFORMAT_PASSARGS(2) ); } template<typename T0 TINYFORMAT_ARGTYPES(3)>
inline void some(const T0& v0 TINYFORMAT_VARARGS(3)) { fprintf(
# 27 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 27 "test_debugger.cpp"
, v0 TINYFORMAT_PASSARGS(3) ); }
int main(int argc, char const *argv[])
{
some( "Something %s.", "New" );
}
更新 2
我试图改变定义的顺序,希望 C 预处理器能正确地扩展,但它只是像以前一样扩展:
#include <stdio.h>
#define TINYFORMAT_ARGTYPES_0
#define TINYFORMAT_ARGTYPES_1 , class T1
#define TINYFORMAT_ARGTYPES_2 , class T1, class T2
#define TINYFORMAT_ARGTYPES_3 , class T1, class T2, class T3
#define TINYFORMAT_VARARGS_0
#define TINYFORMAT_VARARGS_1 , const T1& v1
#define TINYFORMAT_VARARGS_2 , const T1& v1, const T2& v2
#define TINYFORMAT_VARARGS_3 , const T1& v1, const T2& v2, const T3& v3
#define TINYFORMAT_PASSARGS_0
#define TINYFORMAT_PASSARGS_1 , v1
#define TINYFORMAT_PASSARGS_2 , v1, v2
#define TINYFORMAT_PASSARGS_3 , v1, v2, v3
#define TINYFORMAT_FOREACH_ARGNUM(m) \
m(TINYFORMAT_ARGTYPES(0),TINYFORMAT_VARARGS(0),TINYFORMAT_PASSARGS(0)) \
m(TINYFORMAT_ARGTYPES(1),TINYFORMAT_VARARGS(1),TINYFORMAT_PASSARGS(1)) \
m(TINYFORMAT_ARGTYPES(2),TINYFORMAT_VARARGS(2),TINYFORMAT_PASSARGS(2)) \
m(TINYFORMAT_ARGTYPES(3),TINYFORMAT_VARARGS(3),TINYFORMAT_PASSARGS(3))
#define FACTORY(argtypes,varargs,passargs) \
template<typename T0 argtypes> \
inline void some(const T0& v0 varargs) \
{ \
fprintf(stderr, v0 passargs); \
}
TINYFORMAT_FOREACH_ARGNUM(FACTORY)
int main(int argc, char const *argv[])
{
some( "Something %s.", "New" );
}
更新 3
正如@aschepler所说,我遗漏了TINYFORMAT_ARGTYPES_ ## n
的定义,这是一个固定版本:
#include <stdio.h>
#define TINYFORMAT_ARGTYPES_0(...)
#define TINYFORMAT_ARGTYPES_1(...) __VA_ARGS__ class T1
#define TINYFORMAT_ARGTYPES_2(...) __VA_ARGS__ class T1, class T2
#define TINYFORMAT_ARGTYPES_3(...) __VA_ARGS__ class T1, class T2, class T3
#define TINYFORMAT_VARARGS_0(...)
#define TINYFORMAT_VARARGS_1(...) __VA_ARGS__ const T1& v1
#define TINYFORMAT_VARARGS_2(...) __VA_ARGS__ const T1& v1, const T2& v2
#define TINYFORMAT_VARARGS_3(...) __VA_ARGS__ const T1& v1, const T2& v2, const T3& v3
#define TINYFORMAT_PASSARGS_0(...)
#define TINYFORMAT_PASSARGS_1(...) __VA_ARGS__ v1
#define TINYFORMAT_PASSARGS_2(...) __VA_ARGS__ v1, v2
#define TINYFORMAT_PASSARGS_3(...) __VA_ARGS__ v1, v2, v3
#define TINYFORMAT_ARGTYPES(n,...) TINYFORMAT_ARGTYPES_ ## n (__VA_ARGS__)
#define TINYFORMAT_VARARGS(n,...) TINYFORMAT_VARARGS_ ## n (__VA_ARGS__)
#define TINYFORMAT_PASSARGS(n,...) TINYFORMAT_PASSARGS_ ## n (__VA_ARGS__)
#define TINYFORMAT_FOREACH_ARGNUM(m,...) \
m(0) m(1,__VA_ARGS__) m(2,__VA_ARGS__) m(3,__VA_ARGS__)
#define FACTORY(n,...) \
template<TINYFORMAT_ARGTYPES(n,__VA_ARGS__)> \
inline void some(TINYFORMAT_VARARGS(n,__VA_ARGS__)) \
{ \
fprintf(stderr, "variadic" TINYFORMAT_PASSARGS(n,,) ); \
}
TINYFORMAT_FOREACH_ARGNUM(FACTORY,)
int main(int argc, char const *argv[]) {
some();
}
扩展到:g++ -o main -E -g -ggdb test_debugger.cpp --std=c++98
# 797 "/usr/include/stdio.h" 3 4
}
# 2 "test_debugger.cpp" 2
# 31 "test_debugger.cpp"
# 31 "test_debugger.cpp"
template<>
inline void some() { fprintf(
# 31 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 31 "test_debugger.cpp"
, "variadic" ); } template< class T1>
inline void some( const T1& v1) { fprintf(
# 31 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 31 "test_debugger.cpp"
, "variadic" , v1 ); } template< class T1, class T2>
inline void some( const T1& v1, const T2& v2) { fprintf(
# 31 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 31 "test_debugger.cpp"
, "variadic" , v1, v2 ); } template< class T1, class T2, class T3>
inline void some( const T1& v1, const T2& v2, const T3& v3) { fprintf(
# 31 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 31 "test_debugger.cpp"
, "variadic" , v1, v2, v3 ); }
int main(int argc, char const *argv[]) {
some();
}
现在的问题是当宏模板扩展为没有任何模板参数的东西时,即 template<> inline void some()
,它会生成以下代码:
template<>
inline void some()
{
fprintf(stderr, "variadic");
}
int main(int argc, char const *argv[])
{
some();
}
这导致 C++ 编译器抛出此错误:
test_debugger.cpp: error: ‘some’ is not a template function
inline void some()
^
test_debugger.cpp: In function ‘int main(int, const char**)’:
test_debugger.cpp: error: ‘some’ was not declared in this scope
some();
^~~~
模板不能有零个模板参数。以 template <>
开头的语法改为用于显式特化:要使用的声明,而不是用于一组特定模板参数的模板。
因此您的零参数版本需要跳过 template <>
部分。你可以这样做:
#define TINYFORMAT_TEMPLATE_HEAD_0(...)
#define TINYFORMAT_TEMPLATE_HEAD_1(...) template < TINYFORMAT_ARGTYPES_1(__VA_ARGS__) >
#define TINYFORMAT_TEMPLATE_HEAD_2(...) template < TINYFORAMT_ARGTYPES_2(__VA_ARGS__) >
#define TINYFORMAT_TEMPLATE_HEAD_3(...) template < TINYFORMAT_ARGTYPES_3(__VA_ARGS__) >
#define TINYFORMAT_TEMPLATE_HEAD(n, ...) TINYFORMAT_TEMPLATE_HEAD_ ## n (__VA_ARGS__)
#define FACTORY(n,...) \
TINYFORMAT_TEMPLATE_HEAD(n,__VA_ARGS__) \
inline void some(TINYFORMAT_VARARGS(n,__VA_ARGS__)) \
{ \
fprintf(stderr, "variadic" TINYFORMAT_PASSARGS(n,,) ); \
}
感谢 @aschepler 的帮助,我也成功地构建了这个更通用的 solution/example:
#include <stdio.h>
#define TINYFORMAT_ARGTYPES_0(...)
#define TINYFORMAT_ARGTYPES_1(begin,end,...) begin __VA_ARGS__ class T1 end
#define TINYFORMAT_ARGTYPES_2(begin,end,...) begin __VA_ARGS__ class T1, class T2 end
#define TINYFORMAT_ARGTYPES_3(begin,end,...) begin __VA_ARGS__ class T1, class T2, class T3 end
#define TINYFORMAT_VARARGS_0(...)
#define TINYFORMAT_VARARGS_1(...) __VA_ARGS__ const T1& v1
#define TINYFORMAT_VARARGS_2(...) __VA_ARGS__ const T1& v1, const T2& v2
#define TINYFORMAT_VARARGS_3(...) __VA_ARGS__ const T1& v1, const T2& v2, const T3& v3
#define TINYFORMAT_PASSARGS_0(...)
#define TINYFORMAT_PASSARGS_1(...) __VA_ARGS__ v1
#define TINYFORMAT_PASSARGS_2(...) __VA_ARGS__ v1, v2
#define TINYFORMAT_PASSARGS_3(...) __VA_ARGS__ v1, v2, v3
#define TINYFORMAT_ARGTYPES(n,begin,end,...) TINYFORMAT_ARGTYPES_ ## n (begin,end,__VA_ARGS__)
#define TINYFORMAT_VARARGS(n,...) TINYFORMAT_VARARGS_ ## n (__VA_ARGS__)
#define TINYFORMAT_PASSARGS(n,...) TINYFORMAT_PASSARGS_ ## n (__VA_ARGS__)
#define TINYFORMAT_FOREACH_ARGNUM(m,...) \
m(0) m(1,__VA_ARGS__) m(2,__VA_ARGS__) m(3,__VA_ARGS__)
#define FACTORY_FULLY_OPTIONAL(n,...) \
TINYFORMAT_ARGTYPES(n,template<,>,__VA_ARGS__) \
inline void some_optional(TINYFORMAT_VARARGS(n,__VA_ARGS__)) \
{ \
fprintf( stderr, "some_optional\n" TINYFORMAT_PASSARGS(n,,) ); \
}
#define FACTORY_WITH_EXISTENT_ARGS(n,...) \
template<class T0 TINYFORMAT_ARGTYPES(n,,,__VA_ARGS__)> \
inline void some_existent(const T0& v0 TINYFORMAT_VARARGS(n,__VA_ARGS__)) \
{ \
fprintf( stderr, "some_existent %s\n", v0 TINYFORMAT_PASSARGS(n,__VA_ARGS__) ); \
}
TINYFORMAT_FOREACH_ARGNUM(FACTORY_FULLY_OPTIONAL,)
TINYFORMAT_FOREACH_ARGNUM(FACTORY_WITH_EXISTENT_ARGS,,)
int main(int argc, char const *argv[])
{
some_optional();
some_existent( "varing" );
}
这是生成的代码:g++ -o main -E -g -ggdb test_debugger.cpp --std=c++98
# 797 "/usr/include/stdio.h" 3 4
}
# 2 "test_debugger.cpp" 2
# 38 "test_debugger.cpp"
# 38 "test_debugger.cpp"
inline void some_optional() { fprintf(
# 38 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 38 "test_debugger.cpp"
, "some_optional\n" ); } template< class T1 >
inline void some_optional( const T1& v1) { fprintf(
# 38 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 38 "test_debugger.cpp"
, "some_optional\n" , v1 ); } template< class T1, class T2 >
inline void some_optional( const T1& v1, const T2& v2) { fprintf(
# 38 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 38 "test_debugger.cpp"
, "some_optional\n" , v1, v2 ); } template< class T1, class T2, class T3 >
inline void some_optional( const T1& v1, const T2& v2, const T3& v3) { fprintf(
# 38 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 38 "test_debugger.cpp"
, "some_optional\n" , v1, v2, v3 ); }
template<class T0 >
inline void some_existent(const T0& v0 ) { fprintf(
# 39 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 39 "test_debugger.cpp"
, "some_existent %s\n", v0 ); } template<class T0 , class T1 >
inline void some_existent(const T0& v0 , const T1& v1) { fprintf(
# 39 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 39 "test_debugger.cpp"
, "some_existent %s\n", v0 , v1 ); } template<class T0 , class T1, class T2 >
inline void some_existent(const T0& v0 , const T1& v1, const T2& v2) { fprintf(
# 39 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 39 "test_debugger.cpp"
, "some_existent %s\n", v0 , v1, v2 ); } template<class T0 , class T1, class T2, class T3 >
inline void some_existent(const T0& v0 , const T1& v1, const T2& v2, const T3& v3) { fprintf(
# 39 "test_debugger.cpp" 3 4
((__getreent())->_stderr)
# 39 "test_debugger.cpp"
, "some_existent %s\n", v0 , v1, v2, v3 ); }
int main(int argc, char const *argv[])
{
some_optional();
some_existent("varing");
}
运行它:./main
some_optional
some_existent varing
相关问题:
- Comma in C/C++ macro
- What are the valid characters for macro names?
- Concatenation of tokens in variadic macros
- Is it possible to iterate over arguments in variadic macros?
- C++ preprocesor macro for accumulating comma-separated strings
- Variadic macros with zero arguments, and commas
- Is it possible to stringify a C macro that contains a comma?
- What type of content is allowed to be used as arguments for C preprocessor macro?
- How to stringify a string which contains a comma?