Concepts Reference
Named requirements for Asio types and functions.
Stream Concepts
AsyncReadStream
A type that supports async read operations.
Requirements:
-
async_read_some(buffers, token)— Initiates a read operation
Models:
-
tcp::socket -
ssl::stream<tcp::socket> -
buffered_read_stream<>
Usage with coroutines:
template<typename AsyncReadStream>
awaitable<std::size_t> read_some(AsyncReadStream& stream)
{
char buf[1024];
co_return co_await stream.async_read_some(
asio::buffer(buf), use_awaitable);
}
Buffer Concepts
ConstBufferSequence
A sequence of read-only buffers.
Requirements:
-
Iterable with
begin()/end() -
Iterator dereferences to something convertible to
const_buffer
Models:
-
const_buffer -
std::array<const_buffer, N> -
std::vector<const_buffer> -
Result of
buffer("string")
MutableBufferSequence
A sequence of writable buffers.
Requirements:
-
Iterable with
begin()/end() -
Iterator dereferences to something convertible to
mutable_buffer
Models:
-
mutable_buffer -
std::array<mutable_buffer, N> -
std::vector<mutable_buffer> -
Result of
buffer(array)
DynamicBuffer
A buffer that can grow to accommodate data.
Requirements:
-
size()— Current size -
max_size()— Maximum allowed size -
capacity()— Current capacity -
data()— Get const buffer sequence -
prepare(n)— Get mutable buffer for writing n bytes -
commit(n)— Mark n bytes as written -
consume(n)— Remove n bytes from front
Models:
-
dynamic_buffer(std::string&) -
dynamic_buffer(std::vector<char>&) -
streambuf
Protocol Concepts
Protocol
Defines a communication protocol (TCP, UDP, etc.).
Requirements:
-
endpoint— Nested endpoint type -
socket— Nested socket type (or viabasic_socket)
Models:
-
tcp -
udp -
icmp -
local::stream_protocol -
local::datagram_protocol
Executor Concepts
Completion Token Concepts
Usage Notes
These concepts are informal—they’re documented requirements, not C++20
concept declarations. Asio uses SFINAE and enable_if for constraint checks.
When writing generic code:
// Works with any AsyncReadStream
template<typename Stream>
awaitable<void> read_and_process(Stream& stream)
{
char buf[1024];
auto n = co_await stream.async_read_some(
asio::buffer(buf), use_awaitable);
// process buf[0..n)
}
For full details on each concept, see the Boost.Asio reference documentation.
Next Steps
-
Introduction — Library overview
-
Initiating Functions — How async operations start