简体   繁体   中英

Pass a constexpr char[] var to template param of struct and then use fmt to output will throw an exception?

example code

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;
}

In practice, This code throws an exception in function 'basic_string_view(const Char* s)', I tried to replace constexpr with const, and then everything is ok. Does anyone know why?

I want to know why constexpr cannot be used here, Is that the rule or a bug?

More Info:

OS: Windows

IDE: vs2019 (16.11.18)

C++ Language Standard: C++20

Fmt version: 9.1.0 (The same problem exists with std::format)

szTest is a valid template non type argument for a parameter of type const char* and the program is well-formed.

Is that the rule or a bug?

Yes, this seems to be a msvc linker bug . Both gcc and clang execute the program without any problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM