GithubHelp home page GithubHelp logo

ctprintf's Introduction

I am providing code in the repository to you under an open source license. Because this is my personal repository, the license you receive to my code is from me and not my employer (Facebook).

Warning

This project is C++ "stunt coding": an exercise in finding out if I could do this.

If you are looking to produce formatted output in a real program I recommend you use the idiomatic C++ mechanism or the idiomatic C mechanism.

What is this?

This is a suite of constexpr format function objects in standard C++17 and a printf-alike user-defined string literal suffix that's implemented with a nonstandard GCC/LLVM C++ extension.

What does printf-alike mean?

% is the special character that means to insert some formatting. The valid format characters are a bit different. Length modifiers (hh, h, l, ll) are never used because C++ allows us to detect the type of the argument.

The valid format charaters right now are:

Character Description
% A literal percent character. No argument.
d Print an integer argument as decimal. Accepts any signed or unsigned integer type. Replaces d, i, u.
x Print an integer argument as lowercase hexadecimal. Accepts any unsigned integer type. Replaces x.
X Print an integer argument as uppercase hexadecimal. Accepts any unsigned integer type. Replaces X.
s Print a string argument. Accepts std::basic_string_view of the same character type as the format string and chacter pointers to NULL-terminated (c-style) strings. Replaces s.

Is it functionally complete?

No. Notably there is no support for rendering floating point numbers, padding or aligning fields, or setting the formatted precision.

Are there any other limitations?

You can probably hit your environment's limits for number of superclasses, number of template parameters, maximum length of a function name, etc.

Usability

If you get anything wrong (wrong argument type, extra arguments, missing arguments, etc) you can expect screens of nested template errors, none of which really tell you what you did wrong.

Performance

In realistic benchmarks it's comparable to C++'s ofstream and slower than C's fprintf. In unrealistic benchmarks it's faster than both, but that's not relevant.

ctprintf's People

Stargazers

 avatar

Watchers

 avatar

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.