Search code examples
c++fmt

Understand fmt formatter parse function


I'm trying to under the parse function for creating a formatter for a custom type in fmt. In their documentation (https://fmt.dev/dev/api.html) there is this line that has some sort of loop construct I haven't seen before:

auto it = ctx.begin(), end = ctx.end();
if (it != end && (*it == 'f' || *it == 'e')) presentation = *it++;

It's obviously a loop using iterators, presumably something new in C++17. What is it? Full example here: https://godbolt.org/z/fEGvaj


Solution

  • The formatter::parse function takes a parse context ctx and checks if the range [ctx.begin(), ctx.end()) contains format specifiers f or e in this example.

    if (it != end && (*it == 'f' || *it == 'e')) presentation = *it++;
        ^                 ^             ^
     check if the         check if the first
     range is empty       character is 'f' or 'e'
    

    There is nothing particularly novel here, this code is compatible with C++98.