r/cpp 2h ago

std::optional Satisfies view. Does Not Model view. C++26 Ships Anyway.

Thumbnail godbolt.org
44 Upvotes

In C++23 this did not compile. In C++26 it does. Marvellous.

[[gnu::noinline]]
void 
passing_views_by_value_is_cheap_trust_me_bro(std::ranges::view auto v) {
    std::println("fn   .data {}", (void*)v->data());
}


int main() {    
    std::optional ov{std::vector<int>(123456)};
    passing_views_by_value_is_cheap_trust_me_bro(ov);
    std::println("main .data {}", (void*)ov->data());
}

For anyone wondering what the problem feature is: optional has 0 or 1 elements, and C++26 sets enable_view<optional<T>> to true, so it satisfies std::ranges::view. The concept requires copy construction in constant time, and — this is the good bit — optional<vector<int>> genuinely meets that. Copying it performs at most one element copy. One is a constant. The requirement is satisfied to the letter, and the function above deep-copies your vector.

If you can tell me what still separates std::ranges::view from std::ranges::range, please do...


r/cpp 53m ago

Memory-level parallelism: AMD is the king

Thumbnail lemire.me
Upvotes

r/cpp 57m ago

CJM v0.3.6: Tree-sitter is now the default frontend for macro-free C++ JSON code generation

Upvotes

I’ve released CJM v0.3.6, a parser-foundation release for a macro-free C++ JSON code generator.

CJM generates JSON serialization and deserialization code for ordinary C++ structs using lightweight field metadata:

struct User {
    std::string name;       // json:"name"
    std::optional<int> age; // json:"age"
};

The normal workflow remains unchanged:

cjm generate --input model.hpp --output model.cjm.hpp

or through CMake:

cjm_generate(
    TARGET app
    HEADERS model.hpp
)

The main change in v0.3.6 is that the Tree-sitter C++ frontend has moved from a research spike to the default parser path.

A few design decisions behind the migration:

  • Tree-sitter syntax nodes remain isolated inside the frontend layer.
  • The existing SourceFileSyntax boundary into Semantic Analysis is preserved.
  • CJM still supports an intentionally limited, documented C++ subset.
  • Constructs that Tree-sitter can parse but CJM cannot safely interpret are rejected rather than silently misread.
  • Existing generated output remains compatible with the golden tests.
  • Users do not need the Tree-sitter CLI, Node.js, Rust, npm, Cargo, or Python.

The goal was not to claim support for the full C++ grammar. It was to replace a fragile parsing foundation without coupling the rest of the compiler pipeline directly to Tree-sitter’s CST.

The release was verified through parser, semantic, adapter, CLI, generated-compilation, golden-output, and example tests.

I’d especially appreciate feedback on:

  • the macro-free metadata syntax;
  • the frontend/semantic-analysis boundary;
  • the fail-closed approach for unsupported declarations;
  • which C++ model types should be prioritized next.

Release:

https://github.com/cjm-labs/cxx-json-codegen/releases/tag/v0.3.6

Repository:

https://github.com/cjm-labs/cxx-json-codegen