c++c++14stdnull-terminatedfmt

How do I print a vector of chars using fmt?


I have a const std::vector<char> - not null-terminated. I want to print it using the fmt library, without making a copy of the vector.

I would have hoped that specifying the precision would suffice, but the fmt documentation says that :

Note that a C string must be null-terminated even if precision is specified.

Well, mine isn't. Must I make a copy and pad it with \0, or is there something else I can do?


Solution

  • tl;dr: std::cout << fmt::format("{}", fmt::string_view{v.data(), v.size()});


    So, fmt accepts two kinds of "strings":

    Since C++17, C++ officially has the reference-type, std::string-like string view class, which could refer to your vector-of-chars. (without copying anything) - and fmt can print these. Problem is, you may not be in C++17. But fmt itself also has to face this problem internally, so it's actually got you covered - in whatever version of the standard you can get fmt itself to compile, in particular C++14:

    const std::vector<char> v;
    fmt::string_view sv(v.data(), v.size());
    auto str = fmt::format("{}", sv);
    std::cout << str;
    

    Thanks @eerorika for making me think of string views.