Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

对即时函数的调用不是常量表达式 #1

Closed
wuxianggujun opened this issue Apr 12, 2024 · 5 comments
Closed

对即时函数的调用不是常量表达式 #1

wuxianggujun opened this issue Apr 12, 2024 · 5 comments

Comments

@wuxianggujun
Copy link

项目采用 c++20
平台 window11
开发工具 Visual Studio 2022

当我直接复制你的代码过去还是会报错,我之前照着几个开源项目学习,每次都是会报错这个。
严重性 代码 说明 项目 文件 行 禁止显示状态 详细信息
错误 C7595 “fmt::v9::basic_format_string<char,const int &>::basic_format_string”: 对即时函数的调用不是常量表达式 C:\Users\wuxianggujun\CodeSpace\CmakeProjects\Tina\out\build\x64-Debug\Tina C:\Users\wuxianggujun\CodeSpace\CmakeProjects\Tina\engine\log\Logger.hpp 283

   template <typename... Args>
   inline void Logger::log(const spdlog::source_loc& loc, LogLevel lvl, const char* fmt,
       const Args&... args)
   {
       spdlog::log(loc, static_cast<spdlog::level::level_enum>(lvl), fmt, args...);
   }
```基本上就是这一句,找了很多方法,没办法只能将你的全部复制试试,还是报错。
@wuxianggujun
Copy link
Author

设置c++版本为17就不会报错了,那如果是要使用c++20有什么方法吗?

@KkemChen
Copy link
Owner

我主要是在linux下,所以msvc没测过20😅

@KkemChen
Copy link
Owner

KkemChen commented Apr 13, 2024

应该是C++20对编译期类型检查更严格了,所以这个报错了。 刚才的commit已经修复了

 template <typename... Args>
 inline void Logger::log(const spdlog::source_loc& loc, LogLevel lvl, const char* fmt, const Args&... args)
 {
-    spdlog::log(loc, static_cast<spdlog::level::level_enum>(lvl), fmt, args...);
+    spdlog::log(loc, static_cast<spdlog::level::level_enum>(lvl), fmt::runtime(fmt), args...);
 }

这样改就可以了

@miyanyan
Copy link

我也遇到了这个问题,C++20开始要执行编译期的检查,https://fmt.dev/latest/api/#core-api

fmt::format_string is a format string which can be implicitly constructed from a string literal or a constexpr string and is checked at compile time in C++20. To pass a runtime format string wrap it in fmt::runtime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants