{"id":13533781,"url":"https://github.com/nonstd-lite/any-lite","last_synced_at":"2026-04-02T01:19:46.690Z","repository":{"id":37385663,"uuid":"70698894","full_name":"martinmoene/any-lite","owner":"martinmoene","description":"any lite - A C++17-like any, a type-safe container for single values of any type for C++98, C++11 and later in a single-file header-only library","archived":false,"fork":false,"pushed_at":"2025-03-18T20:03:46.000Z","size":207,"stargazers_count":138,"open_issues_count":1,"forks_count":26,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-28T23:05:38.916Z","etag":null,"topics":["any","any-implementations","cpp17","cpp98","header-only","no-dependencies","single-file"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsl-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/martinmoene.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.txt","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-12T12:43:17.000Z","updated_at":"2025-03-25T19:01:28.000Z","dependencies_parsed_at":"2024-01-23T17:10:11.734Z","dependency_job_id":"f785b356-e2a4-4585-83b2-f8b18a69c294","html_url":"https://github.com/martinmoene/any-lite","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinmoene%2Fany-lite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinmoene%2Fany-lite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinmoene%2Fany-lite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinmoene%2Fany-lite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/martinmoene","download_url":"https://codeload.github.com/martinmoene/any-lite/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247266563,"owners_count":20910836,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["any","any-implementations","cpp17","cpp98","header-only","no-dependencies","single-file"],"created_at":"2024-08-01T07:01:23.061Z","updated_at":"2025-12-29T23:37:24.443Z","avatar_url":"https://github.com/martinmoene.png","language":"C++","funding_links":[],"categories":["Libraries"],"sub_categories":["C++"],"readme":"# any-lite: A single-file header-only version of a C++17-like any, a type-safe container for single values of any type for C++98, C++11 and later\n\n[![Language](https://img.shields.io/badge/C%2B%2B-98/11/14/17-blue.svg)](https://en.wikipedia.org/wiki/C%2B%2B#Standardization) [![License](https://img.shields.io/badge/license-BSL-blue.svg)](https://opensource.org/licenses/BSL-1.0) [![Build Status](https://github.com/martinmoene/any-lite/actions/workflows/ci.yml/badge.svg)](https://github.com/martinmoene/any-lite/actions/workflows/ci.yml) [![Version](https://badge.fury.io/gh/martinmoene%2Fany-lite.svg)](https://github.com/martinmoene/any-lite/releases) [![download](https://img.shields.io/badge/latest-download-blue.svg)](https://raw.githubusercontent.com/martinmoene/any-lite/master/include/nonstd/any.hpp) [![Conan](https://img.shields.io/badge/on-conan-blue.svg)](https://conan.io/center/any-lite) [![Vcpkg](https://img.shields.io/badge/on-vcpkg-blue.svg)](https://vcpkg.link/ports/any-lite) [![Try it online](https://img.shields.io/badge/on-wandbox-blue.svg)](https://wandbox.org/permlink/GzoTkzwF7t5ncDg0) [![Try it on godbolt online](https://img.shields.io/badge/on-godbolt-blue.svg)](https://godbolt.org/z/2KN7Vx)\n\n**Contents**  \n- [Example usage](#example-usage)\n- [In a nutshell](#in-a-nutshell)\n- [License](#license)\n- [Dependencies](#dependencies)\n- [Installation](#installation)\n- [Synopsis](#synopsis)\n- [Features](#features)\n- [Reported to work with](#reported-to-work-with)\n- [Building the tests](#building-the-tests)\n- [Other implementations of any](#other-implementations-of-any)\n- [Notes and references](#notes-and-references)\n- [Appendix](#appendix)\n\n## Example usage\n\n```Cpp\n#include \"nonstd/any.hpp\"\n\n#include \u003ccassert\u003e\n#include \u003cstring\u003e\n\nusing namespace nonstd;\n\nint main()\n{\n    std::string hello = \"hello, world\";\n\n    any var;\n\n    var =  'v' ; assert( any_cast\u003cchar\u003e( var ) == 'v' );\n    var =   7  ; assert( any_cast\u003cint \u003e( var ) ==  7  );\n    var =  42L ; assert( any_cast\u003clong\u003e( var ) == 42L );\n    var = hello; assert( any_cast\u003cstd::string\u003e( var ) == hello );\n}\n```\n\n### Compile and run\n\n```Text\nprompt\u003e g++ -Wall -I../include -o 01-basic 01-basic.cpp \u0026\u0026 01-basic\n```\n\n## In a nutshell\n\n**any lite** is a single-file header-only library to represent a type-safe container for single values of any type. The library aims to provide a [C++17-like any](http://en.cppreference.com/w/cpp/utility/any) for use with C++98 and later. If available, std::any is used.\n\n**Features and properties of any lite** are ease of installation (single header), freedom of dependencies other than the standard library. *any lite* shares the approach to in-place tags with [expected-lite](https://github.com/martinmoene/expected-lite), [optional-lite](https://github.com/martinmoene/optional-lite) and with [variant-lite](https://github.com/martinmoene/variant-lite) and these libraries can be used together.\n\n**Limitations of any lite** are the absence of small-object optimization: all contained objects are dynamically allocated. Move construction, move assignment and emplacement require C++11 and are not supported when compiling under C++98.\n\n\n## License\n\n*any lite* is distributed under the [Boost Software License](https://github.com/martinmoene/any-lite/blob/master/LICENSE.txt).\n\n## Dependencies\n\n*any lite* has no other dependencies than the [C++ standard library](http://en.cppreference.com/w/cpp/header).\n\n## Installation\n\n*any lite* is a single-file header-only library. Put `any.hpp` in the [include](include) folder directly into the project source tree or somewhere reachable from your project.\n\n## Synopsis\n\n**Contents**  \n- [Types in namespace nonstd](#types-in-namespace-nonstd)  \n- [Interface of *any lite*](#interface-of-any-lite)  \n- [Algorithms for *any lite*](#algorithms-for-any-lite)  \n- [Configuration](#configuration)\n\n### Types and values in namespace nonstd\n\n| Purpose               | Type / value | Notes |\n|-----------------------|--------------|-------|\n| Type-safe container   | class **any**                        | \u0026nbsp; |\n| Error reporting       | class **bad_any_cast**               | \u0026nbsp; |\n| In-place construction | struct **in_place_tag**              | \u0026nbsp; |\n| \u0026nbsp;                | **in_place**                         | select type or index for in-place construction |\n| \u0026nbsp;                | **in_place_type**                    | select type for in-place construction |\n| \u0026emsp;(variant)       | **in_place_index**                   | select index for in-place construction |\n| \u0026nbsp;                | **nonstd_lite_in_place_type_t**( T)  | macro for alias template in_place_type_t\u0026lt;T\u003e  |\n| \u0026emsp;(variant)       | **nonstd_lite_in_place_index_t**( T )| macro for alias template in_place_index_t\u0026lt;T\u003e |\n\n### Interface of *any lite*\n\n| Kind         | Std  | Method                                       | Result |\n|--------------|------|----------------------------------------------|--------|\n| Construction |\u0026nbsp;| **any**()                                        | default-construct             |\n| \u0026nbsp;       |\u0026nbsp;| **any**( any const \u0026 rhs )                       | copy-construct from other any |\n| \u0026nbsp;       | C++11| **any**( any \u0026\u0026 rhs ) noexcept                   | move-construct from other any |\n| \u0026nbsp;       | C++11| template\u003c class ValueType \u003e\u003cbr\u003e**any**( ValueType \u0026\u0026 value ) noexcept | move-assign from value |\n| \u0026nbsp;       | C++11| template\u003c class T \u003e\u003cbr\u003eexplicit **any**( in_place_type_t\u0026lt;T\u003e, Args\u0026\u0026... args ) | in-place-construct type T |\n| \u0026nbsp;       | C++11| template\u003c class T, class U, class... Args \u003e\u003cbr\u003eexplicit **any**( in_place_type_t\u0026lt;T\u003e, std::initializer_list\u0026lt;U\u003e il, Args\u0026\u0026... args ) | in-place-construct type T |\n| \u0026nbsp;       |\u003cC++11| template\u003c class ValueType \u003e\u003cbr\u003e**any**( ValueType const \u0026 value ) | copy-assign from value |\n| \u0026nbsp;       |\u0026nbsp;| ~**any**() | destroy current object |\n| Assignment   |\u0026nbsp;| any \u0026 **operator=**( any const \u0026 rhs ) | copy-assign from other |\n| \u0026nbsp;       | C++11| any \u0026 **operator=**( any \u0026\u0026 rhs ) noexcept | move-assign from other |\n| \u0026nbsp;       | C++11| template\u003c class ValueType, ...\u003e\u003cbr\u003eany \u0026 **operator=**( ValueType \u0026\u0026 rhs ) | (move-)assign from value |\n| \u0026nbsp;       |\u003cC++11| template\u003c class ValueType \u003e\u003cbr\u003eany \u0026 **operator=**( ValueType const \u0026 rhs ) | copy-assign from value |\n| Modifiers    | C++11| template\u003c class T, class... Args \u003e\u003cbr\u003evoid **emplace**( Args \u0026\u0026 ... args ) | emplace type T |\n| \u0026nbsp;       | C++11| template\u003c class T, class U, class... Args \u003e\u003cbr\u003evoid **emplace**( std::initializer_list\u0026lt;U\u003e il, Args\u0026\u0026... args ) | emplace type T |\n| \u0026nbsp;       |\u0026nbsp;| void **reset**() noexcept | destroy contained object |\n| \u0026nbsp;       |\u0026nbsp;| void **swap**( any \u0026 rhs ) noexcept | exchange with other any |\n| Observers    |\u0026nbsp;| bool **has_value**() const noexcept | contains an object |\n| \u0026nbsp;       |\u0026nbsp;| const std::type_info \u0026 **type**() const noexcept | Type of contained object |\n\n### Algorithms for *any lite*\n\n| Kind                      | Std  | Function | Result |\n|---------------------------|------|----------|--------|\n| Create                    |C++11 | template\u003c class T, class ...Args \u003e\u003cbr\u003eany **make_any**( Args\u0026\u0026 ...args ) | in-place construct |\n| \u0026nbsp;                    |C++11 | template\u003c class T, class U, class ...Args \u003e\u003cbr\u003eany **make_any**( std::initializer_list\u0026lt;U\u003e il, Args\u0026\u0026 ...args ) | in-place construct |\n| Access                    |\u0026nbsp;| T **any_cast\u0026lt;T\u003e**( any const \u0026 )  | obtained value |\n| \u0026nbsp;                    |\u0026nbsp;| T **any_cast\u0026lt;T\u003e**( any \u0026 )        | obtained value |\n| \u0026nbsp;                    |C++11 | T **any_cast\u0026lt;T\u003e**( any \u0026\u0026 )       | obtained value |\n| \u0026nbsp;                    |\u0026nbsp;| T const \\* **any_cast\u0026lt;T\u003e**( any const * )  | pointer to obtained value |\n| \u0026nbsp;                    |\u0026nbsp;| T \\* **any_cast\u0026lt;T\u003e**( any * )     | pointer to obtained value |\n| Swap                      |\u0026nbsp;| void **swap**( any \u0026 x, any \u0026 y ) | exchange contents |\n\n### Configuration\n\n#### Tweak header\n\nIf the compiler supports [`__has_include()`](https://en.cppreference.com/w/cpp/preprocessor/include), *any lite* supports the [tweak header](https://vector-of-bool.github.io/2020/10/04/lib-configuration.html) mechanism. Provide your *tweak header* as `nonstd/any.tweak.hpp` in a folder in the include-search-path. In the tweak header, provide definitions as documented below, like `#define any_CPLUSPLUS 201103L`.\n#### Standard selection macro\n\n\\-D\u003cb\u003eany\\_CPLUSPLUS\u003c/b\u003e=199711L  \nDefine this macro to override the auto-detection of the supported C++ standard, if your compiler does not set the `__cpluplus` macro correctly.\n\n#### Select `std::any` or `nonstd::any`\n\nAt default, *any lite* uses `std::any` if it is available and lets you use it via namespace `nonstd`. You can however override this default and explicitly request to use `std::any` or any lite's `nonstd::any` as `nonstd::any` via the following macros.\n\n-D\u003cb\u003eany\\_CONFIG\\_SELECT\\_ANY\u003c/b\u003e=any_ANY_DEFAULT  \nDefine this to `any_ANY_STD` to select `std::any` as `nonstd::any`. Define this to `any_ANY_NONSTD` to select `nonstd::any` as `nonstd::any`. Default is undefined, which has the same effect as defining to `any_ANY_DEFAULT`.\n\n#### Disable exceptions\n\n-D\u003cb\u003eany_CONFIG_NO_EXCEPTIONS\u003c/b\u003e=0\nDefine this to 1 if you want to compile without exceptions. If not defined, the header tries and detect if exceptions have been disabled (e.g. via `-fno-exceptions`). Default is undefined.\n\n#### Disable \\[\\[nodiscard\\]\\]\n\n-D\u003cb\u003eany\\_CONFIG\\_NO\\_NODISCARD\u003c/b\u003e=0\nDefine this to 1 if you want to compile without \\[\\[nodiscard\\]\\]. Note that the default of marking `class bad_any_cast` and function `any_cast()` with \\[\\[nodiscard\\]\\] is not part of the C++17 standard. The rationale to use \\[\\[nodiscard\\]\\] is that unnoticed discarded error values may break the error handling flow.\n\n## Reported to work with\n\nThe table below mentions the compiler versions *any lite* is reported to work with.\n\nOS        | Compiler   | Versions |\n---------:|:-----------|:---------|\nWindows   | Clang/LLVM | ? |\n\u0026nbsp;    | GCC        | 5.2.0 |\n\u0026nbsp;    | Visual C++\u003cbr\u003e(Visual Studio)| 8 (2005), 10 (2010), 11 (2012),\u003cbr\u003e12 (2013), 14 (2015) |\nGNU/Linux | Clang/LLVM | 3.5.0 |\n\u0026nbsp;    | GCC        | 4.8.4 |\nOS X      | ?          | ?   |\n\n## Building the tests\n\nTo build the tests you need:\n\n- [Buck](https://buckbuild.com/) or [CMake](http://cmake.org) version 2.8.12 or later to be installed and in your PATH.\n- A [suitable compiler](#reported-to-work-with).\n\nThe [*lest* test framework](https://github.com/martinmoene/lest)  is included in the [test folder](test).\n\nThe following steps assume that the [*any lite* source code](https://github.com/martinmoene/any-lite) has been cloned into a directory named `c:\\any-lite`.\n\n### Buck\n\n```Text\nany-lite\u003e buck run test/\n```\n\n### CMake\n\n1. Create a directory for the build outputs for a particular architecture.\nHere we use c:\\any-lite\\build-win-x86-vc10.\n\n        ~\u003e cd c:\\any-lite\n        any-lite\u003e md build-win-x86-vc10\n        any-lite\u003e cd build-win-x86-vc10\n\n2. Configure CMake to use the compiler of your choice (run `cmake --help` for a list).\n\n        any-lite\\build\u003e cmake -G \"Visual Studio 10 2010\" ..\n\n3. Build the test suite in the Debug configuration (alternatively use Release).    \n\n        any-lite\\build\u003e cmake --build . --config Debug\n\n4. Run the test suite.    \n\n        any-lite\\build\u003e ctest -V -C Debug\n\nAll tests should pass, indicating your platform is supported and you are ready to use *any lite*.\n\n## Other implementations of any\n\n- Isabella Muerte. [MNMLSTC Core](https://github.com/mnmlstc/core) (C++11).\n- Kevlin Henney. [Boost.Any](http://www.boost.org/doc/libs/1_62_0/doc/html/any.html). Safe, generic container for single values of different value types. 2001.\n\n## Notes and References\n\n[1] CppReference. [Any](http://en.cppreference.com/w/cpp/utility/any).  \n\n[2] ISO/IEC WG21. [N4606, section 20.8 Storage for any type](http://wg21.link/n4606). July 2016.\n\n[3] Beman Dawes and Kevlin Henney. [N3508: Any Library Proposal (Revision 2)](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3508.html). January 2013.\n\n[4] Kevlin Henney. [Boost.Any](http://www.boost.org/doc/libs/1_62_0/doc/html/any.html). Safe, generic container for single values of different value types. 2001.\n\n[5] Kevlin Henney. [Valued Conversions](http://www.two-sdg.demon.co.uk/curbralan/papers/ValuedConversions.pdf) (PDF). C++ report, July, August 2000.\n\n[6] Kevlin Henney. [Substitutability. Principles, Idioms and Techniques for C++](http://www.two-sdg.demon.co.uk/curbralan/papers/accu/Substitutability.pdf#page=60) (PDF). Presented at JaCC, Oxford, 16th September 1999.\n\n[7] Kevlin Henney. [Idioms. Breaking the Language Barrier](http://www.two-sdg.demon.co.uk/curbralan/papers/accu/Idioms.pdf#page=32) (PDF). Presented at the ACCU's C and C++ European Developers Forum, the Oxford Union, Oxford, UK, 12th September 1998.\n\n## Appendix\n\n### A.1 Compile-time information\n\nThe version of *any lite* is available via tag `[.version]`. The following tags are available for information on the compiler and on the C++ standard library used: `[.compiler]`, `[.stdc++]`, `[.stdlanguage]` and `[.stdlibrary]`.\n\n### A.2 Any lite test specification\n\n```Text\nany: Allows to default construct any\nany: Allows to copy-construct from any\nany: Allows to move-construct from any (C++11)\nany: Allows to copy-construct from literal value\nany: Allows to copy-construct from const value\nany: Allows to copy-construct from lvalue references\nany: Allows to move-construct from value (C++11)\nany: Allows to in-place construct from literal value (C++11)\nany: Allows to in-place copy-construct from value (C++11)\nany: Allows to in-place move-construct from value (C++11)\nany: Allows to in-place copy-construct from initializer-list (C++11)\nany: Allows to in-place move-construct from initializer-list (C++11)\nany: Allows to copy-assign from any\nany: Allows to move-assign from any (C++11)\nany: Allows to copy-assign from literal value\nany: Allows to copy-assign from value\nany: Allows to move-assign from value (C++11)\nany: Allows to copy-emplace content (C++11)\nany: Allows to move-emplace content (C++11)\nany: Allows to copy-emplace content from intializer-list (C++11)\nany: Allows to move-emplace content from intializer-list (C++11)\nany: Allows to reset content\nany: Allows to swap with other any (member)\nany: Allows to inspect if any contains a value\nany: Allows to obtain type_info of any's content\nswap: Allows to swap with other any (non-member)\nmake_any: Allows to in-place copy-construct any from arguments (C++11)\nmake_any: Allows to in-place move-construct any from arguments (C++11)\nmake_any: Allows to in-place copy-construct any from initializer-list and arguments (C++11)\nmake_any: Allows to in-place move-construct any from initializer-list and arguments (C++11)\nany_cast: Allows to obtain any's content by value (any const \u0026)\nany_cast: Allows to obtain any's content by value (any \u0026)\nany_cast: Allows to obtain any's content by value (any \u0026\u0026)\nany_cast: Allows to obtain any's content by pointer (any const *)\nany_cast: Allows to obtain any's content by pointer (any *)\nany_cast: Throws bad_any_cast if requested type differs from content type (any const \u0026)\nany_cast: Throws bad_any_cast if requested type differs from content type (any \u0026)\nany_cast: Throws bad_any_cast if requested type differs from content type (any \u0026\u0026)\nany_cast: Throws bad_any_cast with non-empty what()\ntweak header: reads tweak header if supported [tweak]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnonstd-lite%2Fany-lite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnonstd-lite%2Fany-lite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnonstd-lite%2Fany-lite/lists"}