A C++ shim for ergm

This vignette documents the C++ convenience wrappers for developing ERGM terms and proposals using modern C++ while interfacing with ergm's core C data structures.

The API partially wraps the Terms and Proposals APIs and focuses on lightweight wrappers (no ownership) around existing C structs to provide: range-based iteration, safer array handling, and access to ->R list elements/attributes.

WARNING: This API is experimental and is subject to change in response to evolving needs and user feedback, but some effort will be made to maintain backwards compatibility. In particular, see the item about namespace versioning below.

Overview

Core Concepts

Network Wrappers: ErgmCppNetwork and ErgmCppWtNetwork

Headers: #include "cpp/ergm_network.h", #include "cpp/ergm_wtnetwork.h"

These wrap Network (unweighted) and WtNetwork (weighted) to provide edge queries, degree access, and simple iteration.

Example:

// You can also use nw.edges(), though for an undirected network, the following
// code will visit each edge twice, once from each end.
ErgmCppNetwork nw(nwp);
for(Vertex i : nw.nodes()) {
  for(Vertex j : nw.out_neighbors(i)) {
    // process edge i->j
  }
}
ErgmCppWtNetwork nw(nwp);
for(auto [j, w] : nw.neighbors(i)) {
  // weighted edge i->j of weight w
}

for(auto [i, j, w] : nw.edges()) {
  // weighted edge i->j of weight w
}

Model Terms: ErgmCppModelTerm and ErgmCppWtModelTerm

Headers: #include "cpp/ergm_changestat.h", #include "cpp/ergm_wtchangestat.h"

Proposals: ErgmCppProposal and ErgmCppWtProposal

Headers: #include "cpp/ergm_proposal.h", #include "cpp/ergm_wtproposal.h"

Helper macros

The following macros define the C entry point and construct network and model term handles named nw and mt for use in impl:

StorageType can be omitted (i.e., passing only 2 arguments) if no private storage is used.

Weighted counterparts in ergm_wtchangestat.h are prefixed with Wt (e.g., WtC_CHANGESTAT_CPP).

Examples

Binary terms: triangle and cycle counts in src/cpp_changestats.cpp.

Valued terms: transitive weights in src/cpp_wtchangestats.cpp.

Proposal: src/MHproposals_triadic.cpp.

Notes and Best Practices



Try the ergm package in your browser

Any scripts or data that you put into this service are public.

ergm documentation built on Dec. 22, 2025, 5:10 p.m.