A header-only C++17 library for automatic runtime type detection from strings.
typecore inspects a string and returns the most specific type it can be parsed as - a bool, char, integer, floating-point number, or std::string fallback - wrapped in a std::variant. No macros, no dependencies, no allocations on the hot path.
- C++17 or later
- A compiler with
std::from_charssupport for floating-point (GCC 11+, Clang 14+, MSVC 19.24+)
typecore is header-only. Copy include/typecore/typecore.hpp into your project and add the include path to your build system.
# Example with g++
g++ -std=c++17 -Iinclude main.cpp -o mainWith rgcc
Add -Iinclude to your build.json:
{
"language": "c++",
"standard": "c++17",
"compiler": "g++",
"flags": ["-Iinclude"]
}Then run:
rgcc compile ./main.cpp#include <typecore/typecore.hpp>
using namespace typecore;
Value v = detect("42"); // -> unsigned int
Value v = detect("-5"); // -> int
Value v = detect("3.14"); // -> float
Value v = detect("true"); // -> bool
Value v = detect("A"); // -> char
Value v = detect("hello"); // -> std::stringauto v = detect("42");
if (std::holds_alternative<unsigned int>(v))
std::cout << "Got an unsigned int: " << std::get<unsigned int>(v) << "\n";std::cout << to_string(detect("3.14")) << "\n"; // "3.14"
std::cout << to_string(detect("true")) << "\n"; // "true"
std::cout << to_string(detect(" ")) << "\n"; // " "try_detect returns std::optional<Value> and never throws. Useful when working with untrusted input.
auto result = try_detect("42");
if (result.has_value())
std::cout << to_string(*result) << "\n";By default, detect falls back to std::string when no numeric or boolean type matches. Pass strict = true to throw std::invalid_argument instead.
detect("not_a_number", true); // throws std::invalid_argumentDetection proceeds in this order:
| Priority | Type | Example input |
|---|---|---|
| 1 | char (raw whitespace) |
" " (single space) |
| 2 | bool |
"true", "false" |
| 3 | char (single printable) |
"A", "?" |
| 4 | Signed integers (int, long, long long) |
"-5", "-2147483648" |
| 5 | Unsigned integers (unsigned int, unsigned long, unsigned long long) |
"42", "+17", "4294967295" |
| 6 | float |
"3.14", "2.5e-3" |
| 7 | double |
"1.7976931348623157e+308" |
| 8 | std::string (fallback) |
"hello", "2025-04-19" |
Leading whitespace is trimmed before detection, except when the entire input is a single whitespace character, which is preserved as a char.
A leading + sign is accepted for unsigned integer parsing and is stripped before the numeric conversion.
using Value = std::variant<
int, long, long long,
unsigned int, unsigned long, unsigned long long,
float, double,
bool, char,
std::string
>;[[nodiscard]] Value detect(std::string_view input, bool strict = false);Detects the type of input and returns the corresponding Value. Throws std::invalid_argument if input is empty or whitespace-only, or if strict is true and no numeric or boolean type matches.
[[nodiscard]] std::optional<Value> try_detect(std::string_view input) noexcept;Non-throwing wrapper around detect. Returns std::nullopt if detection fails for any reason.
[[nodiscard]] std::string to_string(const Value &v);Converts any Value back to a human-readable string. Booleans produce "true" / "false", characters produce a single-character string, and floating-point values are formatted with std::defaultfloat.
template <typename T>
const char *type_name() noexcept;Returns the name of a supported type as a null-terminated string. Useful for diagnostics and logging.
The repository ships with a build.json descriptor and an example driver in examples/basic.cpp.
g++ -std=c++17 -Iinclude examples/basic.cpp -o dist/basic.exeExpected output:
=== typecore::detect Demo ===
TYPE INPUT VALUE
--------------------------------------------------------------------------------
[unsigned int] "42" 42
[int] "-5" -5
[unsigned int] "+17" 17
[float] "3.14" 3.14
...
g++ -std=c++17 -Iinclude test.cpp -o test && ./testAll assertions are compile-time selectable and the test binary exits with code 0 on success.
MIT License - free to use in commercial and personal projects.
Made with ❤️ for clean C++ code.