Giter Site home page Giter Site logo

Comments (3)

c42f avatar c42f commented on August 15, 2024

Sounds like a useful and interesting challange. The tricky bits would be (1) gracefully degrading for compliers without constexpr support, and for dynamic format strings, and (2) implementing it all without bloating up the codebase too much more. The first link gives me hope that it's actually quite simple.

from tinyformat.

c42f avatar c42f commented on August 15, 2024

I had a look at this. It's fairly easy (if cumbersome) to implement a constexpr function which counts the number of format specs in a string at compile time:

constexpr int countFormatSpecs(const char* str, int i, int N)
{
    return
        /*if*/ (i >= N-1) ? (
            /*if*/ (str[i] != '%') ?
                0
            /*else*/ :
                throw std::logic_error("Format string prematurely terminated with a '%'")
        )
        /*else*/ : (
            /*if*/ (str[i] == '%') ? (
                /*if*/ (str[i+1] == '%') ? (
                    countFormatSpecs(str, i+2, N)
                )
                /*else*/ : (
                    1 + countFormatSpecs(str, i+1, N)
                )
            )
            /*else*/ : (
                countFormatSpecs(str, i+1, N)
            )
        )
    ;
}

template<int N>
constexpr int countFormatSpecs(const char (&str)[N])
{
    return countFormatSpecs(str, 0, N-1);
}

Unfortunately this was about as far as I got. Ideally, we'd have the following function overloads:

// Usual version, with runtime format string
template<typename... Args>
void printf(const char* fmt, const Args&... args)
{
    // ...
}

// Version for compile time checking of format string (invalid C++)
template<int N, typename... Args>
void printf(constexpr char (&fmt)[N], const Args&... args)
{
    static_assert(countFormatSpecs(fmt) == sizeof...(args));
    // ...
}

However, there's no such thing as a constexpr function argument, so the second function here can't be written in this form. In fact, I can't see any way to write it while preserving the usual syntax: printf() itself is clearly not a constexpr function, so any compile time checking would presumably have to be done outside. However, it's only inside printf() that we know the number of arguments passed. The only way I can see to combine these things is in the type signature, which at a quick glance is what the author of safe_printf seems to have done. This is clever, but the unfortunately ugly and non-printf compatible syntax which results is a dealbreaker for tinyformat.

Having said that, it might still be possible to achieve a syntax like

printf(FMT("foo %d"), 100);

which could indeed be useful if you were using tinyformat via a logging macro. Of course, FMT would have to be something longer in practice, at least by default.

In all this, it's worth keeping in mind that constexpr functions have the interesting property that they must be callable at runtime as well as compile time. This means none of the usual compile time template tricks work on the arguments of constexpr function since those arguments themselves aren't constexpr variables.

from tinyformat.

c42f avatar c42f commented on August 15, 2024

Here's an amusing thought... we could likely implement this with nice syntax if we were willing to use macros 😬

For example

format_("foo %d", 100);
// expand to something like
format(check_format("foo %d", 100), 100);

wherecheck_format returns "foo %d", or produces a compile time error if the arguments don't match.

from tinyformat.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.