在我当前的项目中,我编写了一个使用
std::format
的记录器。去年它一直有效,但 MSVC 14.36.32532 似乎已经破坏了它。我想弄清楚我的代码是否错误或者我是否发现了编译器/STL bug。
我的记录器位于它自己的模块中,如下所示:
// log.ixx
module;
#include <format>
#include <source_location>
#include <string_view>
export module log;
namespace example {
export template<typename... Ts>
using format_string = std::format_string<Ts...>;
export template <typename... Ts>
struct log_trace {
log_trace(format_string<Ts...> fmt, Ts&&... ts, const std::source_location& loc = std::source_location::current())
{
auto message = std::format(fmt, std::forward<Ts>(ts)...);
// Do stuff with the message. This is not relevant to the question, because
// it currently is commented out anyway
}
};
// Used so we can have a defaulted std::source_location with variadic templates
export template <typename... Ts>
log_trace(format_string<Ts...> fmt, Ts&&...) -> log_trace<Ts...>;
// Used so we can have a defaulted std::source_location with variadic templates
export template <typename... Ts>
log_trace(const char* fmt, Ts&&...) -> log_trace<Ts...>;
// same for log_error, log_warning, ...
}
我这样使用它:
// main.cpp
import log;
int main(int, char**) {
example::log_trace("Hello!");
return 0;
}
在最近的 MSVC 更新之前,这一直运行良好。现在我收到与
operator ""sv
: 有关的错误
1>[...]\MSVC\14.36.32532\include\format(2430,1): error C3688: invalid literal suffix 'sv'; literal operator or literal operator template 'operator ""sv' not found
1>[...]\MSVC\14.36.32532\include\format(2432,1): error C3688: invalid literal suffix 'sv'; literal operator or literal operator template 'operator ""sv' not found
1>[...]\MSVC\14.36.32532\include\format(2434,1): error C3688: invalid literal suffix 'sv'; literal operator or literal operator template 'operator ""sv' not found
1>[...]\MSVC\14.36.32532\include\format(2437,1): error C3688: invalid literal suffix 'sv'; literal operator or literal operator template 'operator ""sv' not found
这可以通过在模块的所有使用者中包含
<string_view>
来解决(即 main.cpp
,这显然不理想。
是我的代码错误还是我发现了 MSVC/STL 错误?
这似乎是一个编译器错误;在最新版本的 MSVC 中,该错误已消失。