Is there any implementation of safe types checked by printf print time?

I have seen many printf type implementations, but most of them use an exception to raise type errors.

As an exercise, I used a formatting prototype like printf using string literals, and it seems to solve all the problems with the old, good printf family (except for using a format read from an external source, which is always unsafe).

Example:

int main(int argc, char* argv[])
{
    std::cout << "const char*: %s, std::string: %s\n"_format("ONE", std::string{"TWO"});
    std::cout << "user defined: %*\n"_format(std::complex<int>{1, 2});
    std::cout << "hex: 0x%x, int: %d\n"_format(16, 123);
    std::cout << "double.2: %.2f, double: %f\n"_format(13.123123, 12.1);

    std::string s = "p(%d, %d)\n"_format(123, 234);
    std::cout << s;

//    not yet working
//    int x, y;
//    std::cin >> "p(%d, %d)"_format(x, y);
//    "p(%d, %d)"_format(x, y) = "p(999, 888)";
}

Full, dirty and not full or optimized code here

.s , , const, , , argv. count , .

, : - , , ++?

+4
1

? : GCC ( ) printf , .

++? Bjarne iostream, IO, , .

+2

Source: https://habr.com/ru/post/1688994/


All Articles