Giter Club home page Giter Club logo

constexpr-to-string's Introduction

constexpr-to-string

Requires C++14 or later.

Features:

  • Convert any integral type to a string at compile-time
  • Supports converting to bases 2 through 36
  • No external dependencies
  • Supports custom character types, e.g. to_string<123, 10, wchar_t>
  • C++20: Supports floating-point-to-string conversion with f_to_string

How to use:

The file to_string.hpp provides a to_string utility, which may be used as below:

const char *number = to_string<2147483648999954564, 16>; // produces "1DCD65003B9A1884"
puts(number);
puts(to_string<-42>); // produces "-42"
puts(to_string<30, 2>); // produces "11110"

With to_string, all that will be found in program disassembly are the resulting string literals, as if you wrote the strings yourself.

Try it on Compiler Explorer.

f_to_string.hpp, requiring C++20, provides an f_to_string utility for floating-point conversion:

puts(f_to_string<3.1415926>); // Defaults to 5-point precision: "3.14159"
puts(f_to_string<{3.1415926, 7}>); // Specify precision: "3.1415926"

How it works

C++14 greatly expanded the capabilities of compile-time code execution through constexpr. In particular, it allows for non-trivial constructors to be constexpr.

to_string takes advantage of this by providing an object that converts a template-parameter integer to a string using a basic itoa implementation in the constructor. Through an additional constexpr member function, we can calculate the length of the resulting string; this can be used to size the object's string buffer for a perfect fit.

Beyond this, to_string simply provides familiar member functions that allow for iteration and data access. The expansion of the capabilities of auto in C++14 help make these definitions concise.

The floating-point implementation f_to_string takes a similar approach, but requires C++20 as it needs a double_wrapper object to capture the double value. double and float cannot directly be template parameters as of C++20, and a non-type template parameter like the double_wrapper structure was not allowed before C++20.

constexpr-to-string's People

Contributors

tcsullivan avatar secmeant avatar enilpajic avatar neargye 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.