Implementation:Tencent Ncnn SimpleSTL
| Knowledge Sources | |
|---|---|
| Domains | C++ Standard Library, Embedded Systems |
| Last Updated | 2026-02-09 19:00 GMT |
Overview
Provides a minimal replacement for the C++ Standard Template Library (STL), implementing the core container classes and utilities that ncnn requires, for use on platforms without a C++ standard library.
Description
The simplestl module is a single header file (simplestl.h, 598 lines) that is conditionally included when NCNN_SIMPLESTL is enabled. It implements the following within the std namespace:
Utility functions:
- std::min, std::max, std::swap -- standard template utility functions.
- std::make_pair -- creates a pair from two values.
Data structures:
- std::pair<T1, T2>: A two-element tuple with first and second members and full comparison operator support.
- std::list<T>: A doubly-linked list with iterator support (iter_list<T>), push_back, push_front, pop_front, erase, clear, and sort operations. Uses node<T> internally with prev/next pointers.
- std::vector<T>: A dynamically-resizing array with push_back, resize, insert, erase, pop_back, clear, data(), size(), empty(), begin(), end(), and back(). Uses a growth factor of 2x via try_alloc() with raw memory allocation and placement construction.
- std::stack<T>: A LIFO adapter backed by std::vector<T> with push, pop, top, empty, size, and clear.
- std::string: Derived from std::vector<char> with c_str(), operator==, operator!=, operator+=, and operator+ for concatenation.
Comparators:
- std::greater<T>, std::less<T> -- functor comparators for use with partial_sort.
Sorting:
- std::partial_sort -- currently implemented via bubble sort (the code contains a TODO noting that heap sort should be used).
Memory management:
- Custom operator new / operator delete overloads (including placement new and C++14 sized deallocation variants) that delegate to malloc / free when NCNN_SIMPLESTL is enabled. When NCNN_SIMPLESTL is not enabled, the standard <new> header is included instead.
Usage
This module is used automatically when building ncnn with NCNN_SIMPLESTL=ON for bare-metal and embedded platforms that have no C++ standard library (libstdc++ or libc++). ncnn's code uses std::vector, std::string, etc. which are transparently provided by this header.
Code Reference
Source Location
- Repository: Tencent_Ncnn
- File: src/simplestl.h
Signature
namespace std {
// Utility functions
template<typename T> const T& max(const T& a, const T& b);
template<typename T> const T& min(const T& a, const T& b);
template<typename T> void swap(T& a, T& b);
template<typename T1, typename T2> pair<T1, T2> make_pair(const T1& t1, const T2& t2);
// Sorting
template<typename RandomAccessIter, typename Compare>
void partial_sort(RandomAccessIter first, RandomAccessIter middle,
RandomAccessIter last, Compare comp);
// Container classes
template<typename T> struct vector; // dynamic array
template<typename T> struct list; // doubly-linked list
template<typename T> struct stack; // LIFO stack (backed by vector)
struct string; // string class (backed by vector<char>)
// Comparators
template<typename T> struct greater;
template<typename T> struct less;
} // namespace std
// Memory management (when NCNN_SIMPLESTL is enabled)
void* operator new(size_t size);
void* operator new[](size_t size);
void* operator new(size_t size, void* ptr); // placement new
void operator delete(void* ptr);
void operator delete[](void* ptr);
Import
#include "simplestl.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| size | size_t | For operator new | Number of bytes to allocate |
| ptr | void* | For placement new | Pre-allocated memory address |
| value | const T& | For container operations | Value to insert or compare |
Outputs
| Name | Type | Description |
|---|---|---|
| return (operator new) | void* | Pointer to allocated memory |
| return (vector::data) | T* | Pointer to the underlying contiguous array |
| return (string::c_str) | const char* | Null-terminated C string |
Usage Examples
Transparent Container Usage
// ncnn code uses standard STL types, which are
// transparently provided by simplestl.h on bare-metal platforms
#include "simplestl.h"
std::vector<int> indices;
indices.push_back(0);
indices.push_back(1);
indices.resize(10, -1);
std::string name = "conv1";
const char* cname = name.c_str();
Pair and List Usage
#include "simplestl.h"
std::pair<int, float> p = std::make_pair(42, 3.14f);
std::list<int> values;
values.push_back(1);
values.push_back(2);
values.push_back(3);