如何简化此函数的调用
How can I simplify the calling of this function?
我已经编写(并使用(了自己的字符串格式化函数,我想以下面所示的特定方式简化该函数的使用,但我不确定如何简化。
这是相关代码:
// Object that can hold a copy of every type I want to print.
// Stores the copy in a union, with an enumeration to identify
// the type. any uses C++ constructors, but could also be implemented
// with C99 designated initializers, like so: https://ideone.com/ElQgBV
struct any
{
...
}
// The string format function requires the variable arguments
// to all be of the 'any' type for type safety and (essential for
// my purposes) positional printing.
// Arguments are accessed with a va_list, so essentially
// the variable arguments are treated as an array of any objects.
char* format_function_(const char* fmt, ...);
// I call the above function with this macro that expands the
// variable arguments and adds a default-constructed sentinel
// at the end. The sentinel is used by the function to count
// how many arguments were passed.
#define format(fmt, ...) format_function_(fmt, __VA_ARGS__, any())
// Calling the function like so, via the above macro...
char* str = format("bits:%4b string:%1 %0 int:%3h float:%2.2n",
any("world"), any("hello"), any(3.14159f), any(42), any((u8)(1<<4)));
// ...returns this string:
// bits:00010000 string:hello world int:0000002A float:3.14
我希望能够像常规*printf
样式的函数一样调用该函数。。。
char* str = format("bits:%4b string:%1 %0 int:%3h float:%2.2n",
"world", "hello", 3.14159f, 42, (u8)(1<<4));
使用隐藏在另一个宏后面的any
对象。
我怎样才能做到这一点?
编辑/更新位置参数对我来说是必不可少的。任何不保留此功能的答案都不是有效答案。
由于C++11标准,有一种叫做参数包的东西,这使得它非常简单:
char* format_function(const char* fmt, ...)
{
...
}
template<typename ...T>
char* format(const char* fmt, T... values)
{
return format_function(fmt, any(values)...);
}
...
char* str = format("bits:%4b string:%1 %0 int:%3h float:%2.2n",
"world", "hello", 3.14159f, 42, (u8)(1<<4));
也许你会喜欢这样的东西?(警报:C++11代码!(
#include <stdio.h>
inline void format() {}
void format(char ch) {
fputc(ch, stdout);
}
void format(int i) {
if(i < 0) {
fputc('-', stdout);
i = -i;
}
int divider = 1;
while(i / divider >= 10)
divider *= 10;
do {
int digit = i / divider;
i -= divider * digit;
divider /= 10;
fputc('0' + digit, stdout);
} while(divider > 0);
}
void format(const char *str) {
fputs(str, stdout);
}
// TODO: Add more 'format()' overloads here!
template<typename FirstArg, typename... OtherArgs>
inline void format(const FirstArg &first, OtherArgs... others) {
format(first);
format(others...);
}
然后,你可以简单地。。。
const char *glorifiedIndex(int index) {
switch(index % 10) {
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
default:
return "th";
}
}
int main(int argc, const char *const argv[]) {
format("Hello, world!n");
format("My name is ", argv[0], ", and I was given ", argc - 1, " argument", argc != 2 ? "s" : "", ".nn");
for(int i = 1; i < argc; i++)
format(i, glorifiedIndex(i), " argument: "", argv[i], ""n");
format("Goodbye, world!n");
}
这是一个更灵活和优雅的模型,原因如下:
- 语义安全
- 类型安全
- 没有
<cstdarg>
的东西 - 没有
any
的东西 - 没有设计得非常糟糕的
iostream
东西 - 它太简单了,无法实现,我的意思是太多:(。将这几行代码与典型的3000多行长的
printf.c
进行比较。差别在几个数量级 - 您可能会有与Java和Python相关的怀旧时刻
- 如果出于任何原因更改任何表达式的类型(即
int
到unsigned
(,则函数会适应这种情况 - (好的和坏的(编译器优化可以很容易地发挥作用
- 库的用户可以通过用用户定义的类型重载
format()
函数来扩展其功能 - 这就不可能使用动态格式(这是出于明显的安全原因(
- 这迫使您为我所说的位打印创建特殊功能,即以机器可解析的方式打印,而不是像
format()
那样由人可读 - 您可以使用重载功能自行扩展此列表:(
相关文章:
- 如何用参数值调用函数(仅在运行时已知)
- 从python中调用C++函数并获取返回值
- 当使用通配符和null指针调用函数时,对输出的说明
- 从R调用C++函数并对其进行集成时出错
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 如何在qt中从另一个类调用函数
- 在 COUT 语句中使用 COUT 调用函数
- 如何从线程中的不同模块调用函数?
- C++从函数指针数组调用函数
- 当 A 在 for 循环中调用函数 B 时,如何计算函数 A 的空间复杂度?
- 如何在 C/C++ 中从外部库调用函数
- 如何使用运算符在同一行中多次调用函数
- 是否可以创建一个从不同类调用函数的线程?
- 无法为类成员调用函数
- 如何从另一个标头 c++ 调用函数
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- 如何只允许在调用函数 B 后调用函数 A?
- 我可以这样调用函数吗?
- 如何在 c++ 的类中递归调用函数方法?
- 为什么在指向对象的迭代器上调用函数不允许我更改对象本身?