C 格式字符串宏
C++ Format String Macro
我的功能可以格式化字符串:
template<typename ... Args>
inline std::string format(const std::string & format, Args ... args)
{
std::size_t size = MINIGINE_FORMAT_PRINTF(nullptr, 0, format.c_str(), args ...) + 1; // +1 for NULL terminated
std::unique_ptr<char[]> buf( new char[ size ] );
MINIGINE_FORMAT_PRINTF(buf.get(), size, format.c_str(), args ...);
return std::string(buf.get(), buf.get() + size - 1); // -1 we don't want NULL terminated
}
问题是当我调用此功能时:
format("%d", "");
警告显示在模板函数中,而不是在呼叫站点中显示。
是否可以在呼叫站点中显示格式警告,而不是模板函数?
我提出了解决我问题的解决方案。如果编译器为格式生成警告,则在调用宏的行中显示警告。
。我已经测试了模板格式函数和下面的宏:https://godbolt.org/当模板函数的格式参数为const char*而不是std :: string
时,它们似乎都会生成相同的代码。#pragma once
#include <string>
#include <memory>
#ifdef _MSC_VER
#define MINIGINE_FORMAT_PRINTF _snprintf
#else
#define MINIGINE_FORMAT_PRINTF snprintf
#endif
#define MINIGINE_FORMAT(format, ...)
/*Capture everything by reference*/[&] /*No arguments*/()
{
std::size_t size = MINIGINE_FORMAT_PRINTF(nullptr, 0, format, __VA_ARGS__) + 1;
std::unique_ptr<char[]> buf( new char[ size ] );
MINIGINE_FORMAT_PRINTF(buf.get(), size, format, __VA_ARGS__);
return std::string(buf.get(), buf.get() + size - 1);
} /*Call the lambda*/()
是否可以在呼叫站点中显示格式警告,而不是在模板函数中显示?
是的,您可以使用constexpr
格式字符串检查无宏(几乎)进行此操作。例如:
template <typename S, typename... Args>
inline std::string format(const S &fmt, const Args &... args) {
constexpr bool is_valid = check_format_string<Args...>(fmt);
// Do formatting.
}
其中check_format_string
是一个constexpr
函数,该功能解析并检查格式字符串,而S
是编译时格式字符串。
该技术用于{fmt}库,该库实现了类似python的格式:
// test.cc
#define FMT_STRING_ALIAS 1
#include <fmt/format.h>
std::string s = format(fmt("{2}"), 42);
编译并观察呼叫站点的预期错误:
$ c++ -Iinclude -std=c++14 test.cc
...
test.cc:4:17: note: in instantiation of function template specialization 'fmt::v5::format<S, int>' requested here
std::string s = format(fmt("{2}"), 42);
^
include/fmt/core.h:778:19: note: non-constexpr function 'on_error' cannot be used in a constant expression
ErrorHandler::on_error(message);
^
include/fmt/format.h:2226:16: note: in call to '&checker.context_->on_error(&"argument index out of range"[0])'
context_.on_error("argument index out of range");
^
您仍然需要一个宏来构建编译时字符串,但这在C 20中是不必要的。
免责声明:我是{fmt}的作者。
相关文章:
- 字符串::格式有什么用. 我也没有在互联网上得到它
- 如何检查字符串格式在读取C++文本文件时是否有效?
- 十进制到八进制递归C++.以标准::字符串格式输出
- 如何从字符串格式中提取 3 个整数"v%d.%d.%d"?
- 查找可变参数列表的字符串格式指定符
- 字符串格式替换为 snprintf 中字符中的值
- 使用堆栈将字符串格式的十进制转换为双精度
- 使用字符串格式限制十六进制数字的数量
- 视觉C++:字符串格式
- C++字符串格式实现错误
- 如何在Xcode中将长双精度转换为字符串格式?或如何解决错误"use of undeclared identifier 'to_string' "
- C++以字符串格式 YYYYMMDD 减去天数
- 如何在windows和linux中使用相同的宽字符串格式
- 如何将标准::字符串格式的 JSON 解码为 Boost 属性树
- C++格式的字符串格式
- 使用C++将任何形式的日期-时间字符串格式更改为美国日期-时间格式
- 使用C/C++设置字符串格式
- 如何创建以字符串格式使用给定变量文本的宏(或其他工具)
- 如何转换?toString() 的 C 字符串格式
- 用字符串格式运算符替换C++字符串