by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Patrick Bos
    @egpbos
    i think ideally i'd have a separate process that handles output, to which other processes send their output, via a block of shared memory to prevent copies
    but this is a bit outside of the scope of my project ;)
    Vladyslav
    @vtronko

    Hello, people. How is your day?
    Can I get an aid with my problem? I want compile-time checks of parameters correctness (number of param mismatch etc.) with syntax like fmt::format("The answer is {:d}", "forty-two");. Is that possible? From website I only see alternative format(fmt kind of syntax that switches to compile time checks instead of throwing exceptions.
    I tried DCMAKE_FMT_PEDANTIC=ONdidn't seem to help on both gcc and clang, probably it's doing not what I thought it would.

    Reason to do this is to have compile-time checks for another library that uses fmt (named spdlog) while not changing anything on spdlog side...
    I promise I did research myself prior to asking here, sorry for any trouble. I hope someone else had solution for this, spdlog seems to be quite popular and is mentioned in readme as a fellow library.

    Victor Zverovich
    @vitaut
    You need to wrap the format string in the fmt (or FMT_STRING) macro as described in http://fmtlib.net/latest/api.html#compile-time-format-string-checks
    Vladyslav
    @vtronko
    Thank you. I assumed that's for calming down MSVC, that's what I read in a particular paragraph on your old blog post.
    std branch being only branch with this feature enabled is no more actual?
    I didn't know fmt and FMT_STRING effect is in fact equal when it comes to aiming for compile time checks.
    Now it is all clear to me. Thank you again, Victor.
    Victor Zverovich
    @vitaut
    Compile-time checks are in {fmt} since version 5.0, not only in the std branch. Macros are unfortunately the only portable way to have compile-time strings.
    jbeach
    @jasonbeach
    I'm trying to implement a formatter for a couple of user defined types (namely a Point type (struct with 3 doubles)and a Polygon type which is a std::vector<Point>). I have them both working, but am not sure the one for polygon is implemented as efficiently as it could be:
    template <>
    struct formatter<Polygon> {
      template <typename ParseContext>
      constexpr auto parse(ParseContext &ctx) { return ctx.begin(); }
    
      template <typename FormatContext>
      auto format(const Polygon &p,
        FormatContext &ctx)
      {
        fmt::memory_buffer out;
        bool is_first = true;
        for (auto& vertice : p.vertices)
        {
          if(is_first)
          {
            format_to(out, "{}", vertice);
            is_first = false;
          }
          else
          {
            format_to(out, ", {}", vertice);
          }
        }
        return format_to(ctx.begin(), "{}", to_string(out));
      }
    };
    }
    Is there a better way?
    Victor Zverovich
    @vitaut
    You could make it more efficient by writing directly to the output iterator ctx.out() instead of intermediate buffer.
    jbeach
    @jasonbeach

    So something like:

      template <typename FormatContext>
      auto format(const Polygon &p,
        FormatContext &ctx)
      {
        //fmt::memory_buffer out;
        bool is_first = true;
        for (auto& vertice : p.vertices)
        {
          if(is_first)
          {
            format_to(ctx.begin(), "{}", vertice);
            is_first = false;
          }
          else
          {
            format_to(ctx.begin(), ", {}", vertice);
          }
        }
        return ctx.begin();
      }

    Seems much better. Thanks!

    jbeach
    @jasonbeach

    So something like

      template <typename FormatContext>
      auto format(const :Polygon &p,
        FormatContext &ctx)
      {
        bool is_first = true;
        for (auto& vertice : p.vertices)
        {
          if(is_first)
          {
            format_to(ctx.begin(), "{}", vertice);
            is_first = false;
          }
          else
          {
            format_to(ctx.begin(), ", {}", vertice);
          }
        }
        return ctx.begin();
      }

    seems much better. Thanks!

    Victor Zverovich
    @vitaut
    Almost. You need to update the iterator between the calls to format_to and use out() instead of begin() (the latter is deprecated):
      template <typename FormatContext>
      auto format(const :Polygon &p,
        FormatContext &ctx)
      {
        bool is_first = true;
        auto out = ctx.out();
        for (auto& vertice : p.vertices)
        {
          if(is_first)
          {
            out = format_to(out, "{}", vertice);
            is_first = false;
          }
          else
          {
            out = format_to(out, ", {}", vertice);
          }
        }
        return out;
      }
    gerald5867
    @gerald5867

    hello i tested fmt::snprintf on vs 2017 like this:

    The first one does only remove %s and %d resulting in
    hallo mein name ist : und ich bin jahre altÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ

    The other two replace the format specifier correctly with their value.
    Also neither of them does a null termination?

    Am i missing something ? shouldnt fmt::snprintf work exactly like std::snprintf?

    image.png
    image.png
    Victor Zverovich
    @vitaut
    Please open an issue with a reproducible example. Code is not copyable from images.
    gerald5867
    @gerald5867
    i did made an issue and closed it again because i wanted to ask first if im doing something wrong.
    i did reopen it just now
    Giulio De Pasquale
    @peperunas
    @vitaut hello Victor, I am following your suggestion to create a custom formatter for LLVM's MachineInstr. I am receiving this error (since I'm using C++11): function uses ‘auto’ type specifier without trailing return type. Which return type should I put instead of auto?
    Here's my sample code:
    template <> struct fmt::formatter<llvm::MachineInstr> {
      template <typename ParseContext> constexpr auto parse(ParseContext &ctx) {
        return ctx.begin();
      }
    
      template <typename FormatContext>
      auto format(const llvm::MachineInstr &MI, FormatContext &ctx) {
        return fmt::format_to(ctx.begin(), "{}", to_string(MI));
      }
    };
    Matthias Moulin
    @matt77hias
    Hey, if one just wants to print a string (which can potentially contain formatting related character sequences), should one use fmt::print("{}", str); or fmt::print(str);. For example: Print("({:.1f}, {:.1f})"); works, but it could be a hack as this is not explicitly documented. Furthermore, if both are valid, which one is the most performant and thus recommended?
    Victor Zverovich
    @vitaut
    @matt77hias use fmt::print("{}", str) as with printf and Python's str.format.
    fmt::print("({:.1f}, {:.1f})") won't work: https://godbolt.org/z/YqpUyh
    @peperunas sorry missed your message. You probably figured it out already, but you should put typename FormatContext::iterator there.
    Matthias Moulin
    @matt77hias
    @vitaut Thanks! (Just realized that Print("({:.1f}, {:.1f})"); is my own wrapper handling the no arguments case in a separate overload, my bad.)
    Widualk
    @Widualk
    can someone help me to implement fmt library in visual studio 2019?
    Roman
    @rdxroman
    @vitaut Hi. Could you please elaborate whether std::format will support compile time validation (similar to FMT_STRING)?
    Victor Zverovich
    @vitaut
    Not in C++20.
    Claudiu Andries
    @WiDuAlK12
    what configuration i need to make for build?
    Victor Zverovich
    @vitaut
    Aaron Bishop
    @Erroneous1
    I'd like to explicitly enable narrow to/from wide string conversion for my project using fmt. I added a custom fmt::formatter<std::wstring_view, char> and fmt::formatter<std::string_view, wchar_t>, and more formatters to handle std::wstring, std::string, const wchar_t*, and const char*. But I'm getting a static assertion failure "mixing character types is disallowed". Is there any way to do this besides converting the original string before formatting or the resulting string after formatting?
    Victor Zverovich
    @vitaut
    You'll need to write your own formatting function that applies conversion to string arguments before forwarding them to fmt::format.
    Aaron Bishop
    @Erroneous1
    Thanks. A little more verbose than I'd like but definitely doable.
    arkantos493
    @arkantos493
    https://godbolt.org/z/r9_YuH
    Is it possible to get this code working (without that exception) WITHOUT the need to change the func_sig string (which comes from the __PRETTY_FUNCTION__ macro).
    Victor Zverovich
    @vitaut
    Yes, all you need is to pass the resulting string as an argument instead of the format string: https://godbolt.org/z/hr7Ayt
    i.e. fmt::print(stderr, "{}", to_string(buf));
    arkantos493
    @arkantos493
    Thank you very much for your help. Now it works as intended.
    Andreas K
    @vulptex
    good morning