Skip to content

mikomikotaishi/stdlibx

Repository files navigation

stdlibx

WARNING: As this library is still under development, many features do not yet work, or requires inelegant workarounds or even inclusion of headers to resolve. These issues will only be resolved as development continues.

WARNING: This library is primarily tested with Clang and GCC, and MSVC support is not yet confirmed.

Overview

This is a project that re-exports the entire C++ standard library as modules, in an API more consistent with standard libraries of other languages. The main purpose is to create a standard library experience that is more enjoyable and familiar to use, while remaining compatible (to some degree) with the original standard library.

The motivation for creating this was that at the time, support for import std; was very poor (especially on CMake), a personal dislike of ISO C++'s usage of snake_case for class names, and a dislike of the flat std:: namespace.

The main features of this repackaging of the standard library are:

  • Symbols follow Rust naming conventions (object type names in PascalCase, constants in UPPER_SNAKE_CASE, variables and methods in snake_case)
    • While I personally would have preferred methods to be in camelCase (like in Java), this would have been too much work to rename existing functions and methods, for very little gain.
    • Perhaps in the future I might wrap all standard library classes using private inheritance, but this may be a gargantuan effort and not in the scope of the project at the present.
  • Splitting the standard library into sub-namespaces (in constrast to the ISO C++ std:: namespace which is largely flat).
    • The divisions try to follow the Rust standard library modules, but also take inspiration from the Java standard library.
  • Option to use only core and alloc modules instead of the full standard library, similar to Rust

Pros:

  • A clean API that is more in line with what C++ style should be (in my opinion), as well as that of other languages.
  • More features that ought to be part of the standard library, in a clean, C++-style interface.
  • Consistently updated (but is currently in-development and may be subject to unstable, API-breaking changes).

Cons:

  • Non-standard, obviously. Features are obviously dependent on what is supported by vendors, and different compilers may have different challenges building this library.
  • This library is developed independently. While we accept feature requests, pull requests, and improvements from everyone, do note that support is limited due to the limited resources.
  • Because of its limited development resources, bugs are bound to arise in independently-developed parts.
  • Often relies on cutting or bleeding-edge features, which may not be suitable for stability-priority projects.
  • Seeing as this is a library on top of another library with additional features, it may increase compile times on your project, or even increase binary size. The library takes roughly 20-30 seconds to build on my (modest) hardware on both Clang and GCC.
  • No support for headers. The library likely will not ever support headers, as it makes extensive use of using statements (and is designed to encourage their usage), and any attempt to simultaneously support headers and modules would likely clutter the code with preprocessor directives and ugly macros.

Requires a minimum of C++23. This library is tested with libstdc++ (GCC) as the default standard library. and has not yet been tested with or MSVC STL. This library expects a minimum of C++23 features, but provides feature-test macro guarding as appropriate.

WARNING: Clang libc++ support lags behind GCC libstdc++ tremendously, and some feature-test macros are incorrectly activated on libc++ despite incomplete support. As such, it is recommended to use libstdc++.

Note that currently, Clang module support is far superior to that of GCC, and thus more features are supported on Clang due to certain bugs (such as compiler crashes and conflicting definition loads) on GCC. Many of these are beyond my ability to solve unfortunately.

License

Features

  • Everything included in the C++ standard library
  • Additional ease-of-use classes and Java-style utility classes (stdx::core::System, stdx::core::Integer, stdx::core::Character, stdx::core::Math)
  • An improved chronology library based on java.time.chrono
  • Minimal wrappers over the GNU C library, Win32 API, and Linux headers, where possible (in stdx::os)
  • A JDBC-style ODBC wrapper (with stdx::sql)
  • LINQ-style queries (stdx::linq), wrapping over std::views::*
  • Logging (stdx::util::logging)
  • Minimal wrappers over Lua headers (stdx::lua)
  • And more!

Libraries

The stdlibx library offers the following namespaces/modules:

Core library (module core, namespace core::*)

Core modules of the standard library, not requiring any memory allocation. Provides the most fundamental and low-level features of the C++ standard library.

Allocation library (module alloc, namespace alloc::*)

Allocation modules of the standard library, providing heap allocation and non-trivial data structures.

Disabled when STDLIB_NO_ALLOC is enabled.

Standard library (module stdx, namespace stdx::*)

The full C++ standard library, containing everything provided by core and alloc, as well as additional functionality depending on operating system and runtime. Includes "extensions", i.e. features that are not officially part of the ISO C++ standard library, but ought to be (in my opinion) and have equivalent features standard libraries of other languages.

Disabled when STDLIBX_NO_STD (or STDLIBX_NO_ALLOC) are enabled.

NOTE: Some parts of this library may be third-party or re-exports of existing libraries, and thus not entirely original code. Code that originates from third party will be adequately attributed, but if there are any issues or concerns, please do not hesitate to contact me.

Some third-party libraries used here (as optional dependencies) include:

Example

import stdx;

using stdx::collections::Vector;
using stdx::fs::Begin;
using stdx::fs::DirectoryEntry;
using stdx::fs::DirectoryIterator;
using stdx::fs::End;
using stdx::fs::Path;
using stdx::linq::Query;

int main(int argc, char* argv[]) {
    Path dir = stdx::fs::current_path();

    Vector<DirectoryEntry> files{
        Begin(DirectoryIterator(dir)),
        End(DirectoryIterator(dir)),
        [](const DirectoryEntry& entry) -> bool { return stdx::fs::is_regular_file(entry); }
    };

    // Use LINQ-style query to find the largest .txt file
    DirectoryEntry result = Query<Vector<DirectoryEntry>>::from(files)
        .where([](const DirectoryEntry& entry) -> bool { return entry.path().extension() == ".txt"; })
        .order_by([](const DirectoryEntry& entry) -> i64 { return -stdx::fs::file_size(entry); })
        .select([](const DirectoryEntry& entry) -> Tuple<Path, u64> { return Tuple{entry.path(), stdx::fs::file_size(entry)}; })
        .first_or_default();

    if (result) {
        const auto& [path, size] = *result;
        System::out.println("Largest .txt file: {} ({} bytes)", path.string(), size);
    } else {
        System::out.println("No .txt files found in directory: {}", dir.string());
    }
}

Build

This supports building using CMake and XMake.

Make (calls CMake):

make

CMake:

cmake -S . -B build -G Ninja
cmake --build build

XMake:

xmake

Contributing

Pull requests are always welcome - if you like this library and wish to contribute or solve bugs, feel free to make improvements.

About

Experimental repackaging of the C++ standard library as modules

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages