![](/img/trans.png)
[英]How to pass a const char[] array as constexpr to a template?
[英]Pass a constexpr char[] var to template param of struct and then use fmt to output will throw an exception?
示例代碼
template<const char* str>
class MyStruct
{
public:
void Print()
{
cout << fmt::format("{}", str);
}
};
constexpr char szTest[] = "hello";
int main()
{
MyStruct<szTest> info;
info.Print();
return 0;
}
實際上,此代碼在 function 'basic_string_view(const Char* s)' 中拋出異常,我嘗試將 constexpr 替換為 const,然后一切正常。 有誰知道為什么?
我想知道為什么不能在這里使用 constexpr,這是規則還是錯誤?
更多信息:
操作系統:Windows
IDE:vs2019 (16.11.18)
C++ 語言標准:C++20
fmt版本:9.1.0(std::format存在同樣的問題)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.