macros - 如何使用 C++ 98 标准轻松创建完全“可变参数”函数?
问题描述
库 https://github.com/c42f/tinyformat/blob/2f9335afd9941688e42d60cae5166b9f0600b2d1/tinyformat.h#L1104-L1116使用这个很棒的技巧在 C++ 98 上执行“可变参数”模板:
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)
,第二次用于“可变参数”部分template<TINYFORMAT_ARGTYPES(n)> void printfln(const char* fmt, TINYFORMAT_VARARGS(n))
。
他们需要将printfln
函数分成两部分,因为“可变参数”函数只能接受一个参数,即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" );
}
但是,它不起作用。当if在宏定义之后gcc
找到 , 时,编译器会发疯: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++“可变参数”函数:
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 个“可变参数”模板函数?
作为参考,我正在使用:
$ 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();
^~~~
解决方案
模板不能有零模板参数。以 instead 开头的语法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,,) ); \
}
推荐阅读
- javascript - Vue CLI 3:使用 Foundation 6 进行生产构建
- node.js - 关于 smtp 服务器和 nodemailer 的问题
- css - 需要 Kindle Fire 的准确媒体查询
- reactjs - 反应多个上下文
- scala - 如何解决“crossPaths := false”和“+publish”之间的交互?
- python - 为什么使用 .loc[] 访问 pandas 数据框的列会产生重复的行?
- php - PHP停留在“等待本地主机”
- c# - 如何从自适应卡检测/监听 action.submit
- java - 如何优化此正则表达式以在空格和标点符号上分割线(减去撇号)
- java - 创建后如何更改片段布局