Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

toml11

> 编程语言
Open source

TOML for Modern C++

1.3K stars0 likes1 views
WebsiteGitHub

About

TOML for Modern C++

toml11

日本語版

toml11 is a feature-rich TOML language library for C++11/14/17/20.

  • It complies with the latest TOML language specification.
  • It passes all the standard TOML language test cases.
  • It supports new features merged into the upcoming TOML version (v1.1.0).
  • It provides clear error messages, including the location of the error.
  • It parses and retains comments, associating them with corresponding values.
  • It maintains formatting information such as hex integers and considers these during serialization.
  • It provides exception-less parse function.
  • It supports complex type conversions from TOML values.
  • It allows customization of the types stored in toml::value.
  • It provides some extensions not present in the TOML language standard.

Example

toml
# example.toml
title = "an example toml file"
nums  = [3, 1, 4, 1, 5] # pi!
…

Error messages can also be colorized by calling toml::color::enable().

By using toml::try_parse, you can receive a toml::result> without throwing exceptions.

cpp
const auto input = toml::try_parse("input.toml");
if(input.is_ok())
{
    std::cout  input.toml
   |
 1 | a = 123_456
   |     ^^^^^^^-- the actual type is integer

You can access nested tables or arrays of tables in the same way.

cpp
// [a]
// b = [
//   {c = 42},
//   {c = 54}
// ]
const toml::value input = toml::parse("input.toml");
std::cout (input, "array");
const auto a2 = toml::find(input, "array");
const auto a3 = toml::find(input, "array");
const auto a4 = toml::find>(input, "array");

You can perform advanced type conversions on complex TOML values.

toml
mixed_array = [
    42,
    3.14,
    {a = "foo", b = "bar"}
]
cpp
const toml::value input = toml::parse("input.toml");

const auto mixed = toml::find
    >(input, "mixed_array") (input, "foo");

Using toml::find_or, you can get a default value in case of failure.

cpp
const toml::value input = toml::parse("input.toml");
std::cout `.

```cpp
const toml::value input = toml::parse("input.toml");
std::cout  internal string at line 64 in file main.cpp
   |
 1 | a = 123__456
   |        ^-- invalid underscore
Hint: valid  : -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755
Hint: invalid: _42, 1__000, 0123

If you request a type different from the actual stored type, an error message like this will be displayed:

[error] toml::value::as_string(): bad_cast to string
 --> input.toml
   |
 1 | a = 123_456
   |     ^^^^^^^-- the actual type is integer

Such error messages can also be produced for user-specific algorithm that is not related to TOML syntax.

cpp
const toml::value& a = input.at("a");
if(a.as_integer() ("input.toml");

Here, toml::ordered_value is an alias of toml::basic_value.

Note that, since toml::value uses std::unordered_map, once you convert it to toml::value, then the order of the values will be randomized.

For more details about how to implement type_config variant, please refer to the documentation.

Also, refer to the examples directory for complex use cases such as using multi-precision integers, changing containers, and normalizing Unicode. Use these examples as references for implementing such configurations.

Examples

The examples directory provides various implementation examples in addition to type configurations.

  • boost_container
    • This example shows how to use boost::container containers for array_type and table_type.
  • boost_multiprecision
    • This example demonstrates the use of boost::multiprecision multi-precision numeric types for integer_type and floating_type.
  • parse_file
    • This example includes type conversion implementations, covering slightly more complex cases. The corresponding TOML file is included.
  • reflect
    • This example shows self-type conversion using boost-ext/reflect for user-defined types.
  • unicode
    • This example demonstrates normalizing Unicode strings when searching for keys using uni-algo.

Changes from v3

toml11 v4 introduces several breaking changes.

Efforts have been made to minimize the need for changes for those using simple functionality. However, if you were utilizing advanced features, some adjustments may be necessary.

Nevertheless, we believe that the added or streamlined features will provide enhanced convenience in return.

Breaking Changes

  • Changed branch from master to main.
  • Changed template arguments of toml::basic_value.
  • Removed toml::string and explicitly store formatting information of all the values.
  • Modified arguments of toml::format to accommodate formatting information.
  • Changed default arguments of toml::parse to take toml::spec for specifying TOML version information.
  • Updated the interface of toml::source_location to accommodate multiline regions.
  • Modified arguments of toml::format_error.
  • Renamed toml::format_underline to toml::format_location.
  • Unified control method of toml::color to toml::color::enable/disable().

Added features

  • Added toml::parse_str.
  • Added toml::try_parse.
  • Added support for parsing byte sequences.
  • Added formatting information to toml::value.
  • Changed to saving comments in toml::value by default.
  • Added single_include/toml.hpp.
  • Enabled to use as a precompiled library.

Contributors

I appreciate the help of the contributors who introduced the great feature to this library.

  • Guillaume Fraux (@Luthaf)
    • Windows support and CI on Appvayor
    • Intel Compiler support
  • Quentin Khan (@xaxousis)
    • Found & Fixed a bug around ODR
    • Improved error messages for invalid keys to show the location where the parser fails
  • Petr Beneš (@wbenny)
    • Fixed warnings on MSVC
  • Ivan Shynkarenka (@chronoxor)
    • Fixed Visual Studio 2019 warnings
    • Fix compilation error in `` with MinGW
  • Khoi Dinh Trinh (@khoitd1997)
    • Fixed warnings while type conversion
  • @KerstinKeller
    • Added installation script to CMake
  • J.C. Moyer (@jcmoyer)
    • Fixed an example code in the documentation
  • Jt Freeman (@blockparty-sh)
    • Fixed feature test macro around localtime_s
    • Suppress warnings in Debug mode
  • OGAWA Kenichi (@kenichiice)
    • Suppress warnings on intel compiler
    • Fix include path in README
  • Jordan Williams (@jwillikers)
    • Fixed clang range-loop-analysis warnings
    • Fixed feature test macro to suppress -Wundef
    • Use cache variables in CMakeLists.txt
    • Automate test set fetching, update and refactor CMakeLists.txt
  • Scott McCaskill
    • Parse 9 digits (nanoseconds) of fractional seconds in a local_time
  • Shu Wang (@halfelf)
    • fix "Finding a value in an array" example in README
  • @maass-tv and @SeverinLeonhardt
    • Fix MSVC warning C4866
  • Mohammed Alyousef (@MoAlyousef)
    • Made testing optional in CMake
  • Alex Merry (@amerry)
    • Add missing include files
  • sneakypete81 (@sneakypete81)
    • Fix typo in error message
  • Oliver Kahrmann (@founderio)
    • Fix missing filename in error message if parsed file is empty
  • Karl Nilsson (@karl-nilsson)
    • Fix many spelling errors
  • ohdarling88 (@ohdarling)
    • Fix a bug in a constructor of serializer
  • estshorter (@estshorter)
    • Fix MSVC warning C26478
  • Philip Top (@phlptp)
    • Improve checking standard library feature availability check
  • Louis Marascio (@marascio)
    • Fix free-nonheap-object warning
  • Axel Huebl (@ax3l)
    • Make installation optional if the library is embedded
  • Ken Matsui (@ken-matsui)
    • Support user-defined error message prefix
    • Support dynamic color mode
    • Support std::optional members for TOML11_DEFINE_CONVERSION_NON_INTRUSIVE
    • Make thread_local for color_mode optional
    • Add toml::find_or_default
    • Fix static assertions in success that checks if specified type is void
  • Giel van Schijndel (@muggenhor)
    • Remove needless copy in parse function
  • Lukáš Hrázký (@lukash)
    • Add a parse(FILE *) interface and improve file-related error messages
  • spiderman idog (@spiderman-idog)
    • Fix typo in README
  • Jajauma's GitHub (@Jajauma)
    • Avoid possible lexer truncation warnings
  • Moritz Klammler (@ctcmkl)
    • Many patches in (#200) including:
    • Improve CMake scripts, build process, and test file handling
    • Detect error when discard_comments is accessed
    • And more.
  • Chris White (@cxw42)
    • Fix address-sanitizer error when parsing literal strings having invalid UTF-8 characters
    • Fix function name in error messages
  • offa (@offa)
    • Update checkout action to v3
    • Update Required CMake version
    • Cleanup old CI settings
  • Sergey Vidyuk (@VestniK)
    • Fix for case when vector iterator is raw pointer
  • Kfir Gollan (@kfirgollan)
    • Add installation example with checkinstall and cmake
  • Martin Tournoij (@arp242)
    • Escape control characters in keys
  • @DavidKorczynski
    • Add fuzzing test based on ClusterFuzzLite
  • Esonhugh Skyworship (@Esonhugh)
    • Fix function signature of strerror_r on macos
  • Alberto (@0X1A)
    • Fix issues with CMake package configuration when used with vcpkg
  • Egor Pugin (@egorpugin)
    • Fix incorrect operator<<() argument type that gives build error
  • Andreas Keller (@andreaskeller96)
    • Fix not checking for \r\n when parsing line comments
  • 萧迩珀 (@CDK6182CHR)
    • Support template into_toml members
  • Pino Toscano (@pinotree)
    • Suppress warnings by manually cast file size to std::streamsize
  • Jack W (@jackwil1)
    • Fix typos in documentation template syntax
  • amatej (@kontura)
    • Fix: toml::detail::region::last_ may be used uninitialized
  • Severin Leonhardt (@SeverinLeonhardt)
    • Fix use with CMake 3.21 and older
  • hayt (@hayt)
    • fix: prevent size_t-max length string allocation
  • somebody (@oldoldtea), (lz)
    • Update README for better ToC, fixing example code
  • Sunlight (@SunPodder)
    • Add erase(...) function to ordered_map

Licensing terms

This product is licensed under the terms of the MIT License.

  • Copyright (c) 2017-2024 Toru Niina

All rights reserved.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C++c-plus-plusc-plus-plus-11c-plus-plus-14c-plus-plus-17

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言