Tannic
A C++ Tensor Library
Loading...
Searching...
No Matches
tannic::Expression Concept Reference

Defines the core protocol for all expression-like types in the Tannic Tensor Library. More...

#include <concepts.hpp>

Concept definition

template<typename T>
concept tannic::Expression = requires(const T expression) {
{ expression.dtype() } -> std::same_as<type>;
{ expression.shape() } -> std::same_as<Shape const&>;
{ expression.strides() } -> std::same_as<Strides const&>;
expression.offset();
expression.forward();
}
Defines the core protocol for all expression-like types in the Tannic Tensor Library.
Definition: concepts.hpp:86

Detailed Description

Defines the core protocol for all expression-like types in the Tannic Tensor Library.

This concept specifies the interface required to participate in tensor operations such as broadcasting, slicing, evaluation, and composition. Any type that satisfies this concept is considered a valid expression—this includes tensors, views, lazy expressions, and future compile-time tensor constructs.

Requirements:

A type T satisfies Expression if it provides:

  • dtype() -> tannic::type — the data type of the expression.
  • shape() -> tannic::Shape const& — the shape (dimensions) of the expression.
  • strides() -> tannic::Strides const& — the strides associated with the memory layout.
  • offset() -> std::ptrdiff_t — the memory offset from the buffer pointer.
  • forward() -> tannic::Tensor — the tensor that will express the computation.

The methods dtype(), shape(), and strides() can be constexpr. All major components—such as Tensor, views, and expression trees—must satisfy this concept. This ensures a consistent interface for operations like +, matmul, broadcast, etc.

The Expression concept is designed to be extensible: custom user-defined types can participate in the tensor system by conforming to this protocol, enabling interoperability with native Tannic expressions.

Example:

template<tannic::Expression E>
void print_metadata(E const& expr) {
std::cout << expr.shape() << " | dtype = " << expr.dtype() << '\n';
}