[英]Compile time: call a function based of one of the argument that is a string literals
I am trying to create a function with partial literals and partial non-literals variadic arguments that return a bool and based on the result, I have to forward all with the same arguments and order .我正在尝试创建一个 function 部分文字和部分非文字变量 arguments 返回一个 bool 并且根据结果,我必须转发所有相同的 arguments 和 order 。
Example 1:示例 1:
int x = some_runtime_func(); // evaluated to 20
float y = some_runtime_func(); // evaluated to 10.5
MY_PRINT("{} {}", x, y); // prints 20 10.5
MY_PRINT("%d" %f", x, y); // prints 20 10.5
Note that MY_PRINT is a MACRO function that will pass with __VA_ARGS__
请注意,MY_PRINT 是一个 MACRO function,它将通过
__VA_ARGS__
To achieve this, I will parse and check the string and forwards it to fmt
library functions based on its content.为此,我将解析和检查字符串并根据其内容将其转发给
fmt
库函数。
The underlying function that I envision is something like this (without variable naming change due to __VA_ARGS__
collapse)我设想的底层 function 是这样的(由于
__VA_ARGS__
崩溃而没有变量命名更改)
bool is_valid = my_formatter_checker("{} {}", x, y);
if constexpr(is_valid)
{
fmt::print("{} {}", x, y);
}
else
{
fmt::printf("{} {}", x, y);
}
While the x and y are not compiled time literals, the string is a compiled time literals , and I want to decide whether I use a specific function only based on the string literals.虽然 x 和 y 不是编译时间文字,但字符串是编译时间文字,我想仅根据字符串文字来决定是否使用特定的 function。 So for this example, the parameter x is not even used inside the function, and I only use the first argument
所以对于这个例子,function 内部甚至没有使用参数 x,我只使用第一个参数
And to make it simple, If the string is not a compile-time string, then I will force it to evaluate it at runtime.为简单起见,如果字符串不是编译时字符串,那么我将强制它在运行时对其求值。
const char* vs const char[N]
which I believe can be implemented with templates type traits. const char* vs const char[N]
我相信可以用模板类型特征来实现。
In addition, fmt allows the first argument to be a specific target such as stdout
or stderr
or file.此外,fmt 允许第一个参数是特定目标,例如
stdout
或stderr
或文件。 which is a case where the first argument can be another type that is not a string literals.在这种情况下,第一个参数可以是不是字符串文字的另一种类型。
Example 2:示例 2:
int x = runtime_function(); // returns 20
MY_PRINT(stderr, "{}", x); // prints 20 to stderr
MY_PRINT(stderr, "%d", x); // prints 20 to stderr
For this case, I am using the second argument to the MY_PRINT
since the second argument is the string literals.对于这种情况,我使用
MY_PRINT
的第二个参数,因为第二个参数是字符串文字。
The main question is how to create the function my_formatter_checker
that takes in both a constexpr parameter and non constexpr parameter, parse only the first string argument.主要问题是如何创建接受 constexpr 参数和非 constexpr 参数的 function
my_formatter_checker
,仅解析第一个字符串参数。 I cannot change the call from the function side such that the user of the function does not have to add additional macro or calls.我无法从 function 端更改调用,这样 function 的用户就不必添加额外的宏或调用。 I expect that this function can be implemented preferably with template metaprogramming or constexpr functions since if the argument is not a string literal it can be evaluated at runtime.
我希望这个 function 可以最好地使用模板元编程或 constexpr 函数来实现,因为如果参数不是字符串文字,它可以在运行时进行评估。
One way of doing this is using a class with an implicit consteval
constructor.一种方法是使用带有隐式
consteval
构造函数的 class。 This is what {fmt} does for compile-time format string checks.这就是{fmt}为编译时格式字符串检查所做的。
For example:例如:
struct format_string {
bool is_valid;
const char* str;
consteval format_string(const char* s) : str(s) {
// Implement your compile-time logic here.
is_valid = *s == '{';
}
};
template <typename... T>
std::string my_print(format_string s, T&&... args) {
if (s.is_valid)
fmt::print(fmt::runtime(s.str), std::forward<T>(args)...);
}
For this to work you need to restructure your code to place all the compile-time logic in format_string
's constructor and the runtime logic is in the my_print
function.为此,您需要重组代码以将所有编译时逻辑放在
format_string
的构造函数中,运行时逻辑在my_print
function 中。
https://godbolt.org/z/7qonoPqWr https://godbolt.org/z/7qonoPqWr
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.