Browser Support Test — C++ source
Audit your real browser against 30 modern web capabilities - WebGPU, WASM, WebAuthn, Service Workers and more. Get a weighted readiness score, copy-paste detection snippets, and a shareable link.
This is the C++ implementation — the same logic the interactive tool runs, in a shareable, citable form.
// browser-support-test — C++ polyglot showcase port.
//
// Runtime capability probe, weighted score, and support tier.
//
// This is the C++ sibling of src/tools/BrowserSupportTest.tsx (+ the
// browser-support/* modules) that powers the live tool.
//
// ILLUSTRATIVE PORT: the live tool detects *browser* features at runtime
// (navigator/window probes) — a browser-only concern that cannot run in a
// native C++ process. This port shows the ANALOGOUS pattern for C++: probes
// read compile-time feature-test macros (__cpp_lib_* / __cpp_* and
// __has_include) — the mechanism standard C++ uses to gate library and
// language features, and the direct equivalent of `'feature' in window`.
// The scoring algorithm and tier thresholds below are byte-for-byte the
// same as the TypeScript lib's; only the probe targets differ.
//
// Language: C++ (C++17, standard library only).
// Ported from src/tools/BrowserSupportTest.tsx (+ browser-support/* modules).
// Source: CosmoDev polyglot showcase port.
// License: display source — part of CosmoDev's polyglot tool pages.
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <string_view>
#include <vector>
namespace {
constexpr std::string_view SUPPORTED = "supported";
constexpr std::string_view UNSUPPORTED = "unsupported";
constexpr std::string_view LIMITED = "limited";
constexpr std::string_view UNKNOWN = "unknown";
// One capability the audit can check. `probe` returns true when the
// capability is present in THIS build.
struct Check {
std::string_view id;
std::string_view title;
std::string_view category;
double weight;
bool (*probe)();
};
// Outcome of one probe.
struct FeatureResult {
std::string_view id;
std::string_view status;
};
// A support tier: display label + tone class.
struct Tier {
std::string_view label;
std::string_view tone;
};
// contribution × weight. Same values as src/tools/browser-support/score.ts.
constexpr double contribution(std::string_view status) {
if (status == "supported") return 1.0;
if (status == "limited") return 0.5;
if (status == "needs-https") return 0.3;
return 0.0; // unsupported, unknown
}
// Weighted readiness 0–100; matches computeScore() exactly. Results whose
// id isn't in the catalog are ignored.
int compute_score(const std::vector<FeatureResult>& results,
const std::vector<Check>& catalog) {
double earned = 0.0;
double possible = 0.0;
for (const auto& r : results) {
// Look up this result's weight in the catalog (skip unknown ids).
for (const auto& c : catalog) {
if (c.id == r.id) {
earned += contribution(r.status) * c.weight;
possible += c.weight;
break;
}
}
}
if (possible == 0.0) return 0;
return static_cast<int>(earned / possible * 100.0 + 0.5); // round half up
}
// tier_for mirrors tierFor() in the island — same thresholds.
Tier tier_for(int score) {
if (score >= 90) return {"Fully modern", "text-success"};
if (score >= 75) return {"Very capable", "text-success"};
if (score >= 55) return {"Capable, minor gaps", "text-accent"};
if (score >= 35) return {"Showing its age", "text-fg-muted"};
return {"Legacy browser", "text-danger"};
}
// Probes. Feature-test macros are resolved at compile time by the
// toolchain — standard C++'s equivalent of Rust's cfg!() / the browser's
// `'feature' in window`.
bool probe_filesystem() {
#if defined(__cpp_lib_filesystem)
return true;
#else
return false;
#endif
}
bool probe_variant() {
#if defined(__cpp_lib_variant)
return true;
#else
return false;
#endif
}
bool probe_string_view() {
#if defined(__cpp_lib_string_view)
return true;
#else
return false;
#endif
}
bool probe_to_chars() {
#if defined(__cpp_lib_to_chars)
return true;
#else
return false;
#endif
}
bool probe_if_constexpr() {
#if defined(__cpp_if_constexpr)
return true;
#else
return false;
#endif
}
bool probe_structured_bindings() {
#if defined(__cpp_structured_bindings)
return true;
#else
return false;
#endif
}
bool probe_fold_expressions() {
#if defined(__cpp_fold_expressions)
return true;
#else
return false;
#endif
}
// C++17 unimplemented-PSTL opt-out is rare — most toolchains leave this
// undefined, giving the audit a realistic "unsupported" row.
bool probe_parallel_algorithms() {
#if defined(__cpp_lib_parallel_algorithm)
return true;
#else
return false;
#endif
}
// Representative catalog of feature-test-gated capabilities.
std::vector<Check> checks() {
return {
{"filesystem", "std::filesystem", "Runtime", 3.0, probe_filesystem},
{"variant", "std::variant", "Data", 2.0, probe_variant},
{"string-view", "std::string_view", "Data", 2.0, probe_string_view},
{"to-chars", "std::to_chars", "Data", 2.0, probe_to_chars},
{"if-constexpr", "if constexpr", "Language", 2.0, probe_if_constexpr},
{"structured", "Structured bindings", "Language", 2.0, probe_structured_bindings},
{"fold", "Fold expressions", "Language", 1.0, probe_fold_expressions},
{"parallel", "Parallel algorithms", "Runtime", 2.0, probe_parallel_algorithms},
};
}
// Run probes; a throw degrades to 'unknown' so one failure can't abort the audit.
std::vector<FeatureResult> run_checks(const std::vector<Check>& catalog) {
std::vector<FeatureResult> out;
out.reserve(catalog.size());
for (const auto& c : catalog) {
std::string_view status = UNKNOWN;
try {
status = c.probe() ? SUPPORTED : UNSUPPORTED;
} catch (...) {
// keep 'unknown'
}
out.push_back({c.id, status});
}
return out;
}
const char* host_os() {
#if defined(__APPLE__)
return "macOS";
#elif defined(_WIN32)
return "Windows";
#elif defined(__linux__)
return "Linux";
#elif defined(__unix__)
return "Unix";
#else
return "unknown";
#endif
}
} // namespace
int main() {
const auto catalog = checks();
const auto results = run_checks(catalog);
const int score = compute_score(results, catalog);
// Structured bindings — itself one of the probed C++17 features, live.
const auto [label, tone] = tier_for(score);
const auto supported = std::count_if(
results.begin(), results.end(),
[](const FeatureResult& r) { return r.status == SUPPORTED; });
std::cout << "browser-support-test (C++17) on " << host_os() << "\n";
std::cout << supported << "/" << results.size()
<< " capabilities · readiness " << score << " · " << label << "\n";
// Silence unused warning when tone/LIMITED are intentionally unread here.
static_cast<void>(tone);
static_cast<void>(LIMITED);
return 0;
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →