dimval 0.2.0
Modern C++23 header-only library of dimensional values (units, measures, ranges)
Loading...
Searching...
No Matches
dimval::UnitValue< U, T > Struct Template Reference

A value paired at the type level with a unit tag. More...

#include <unit.hpp>

Inheritance diagram for dimval::UnitValue< U, T >:
Collaboration diagram for dimval::UnitValue< U, T >:

Public Member Functions

constexpr UnitValue (T value) noexcept
 Implicit conversion from the numeric type — enables MeterValue distance = 1.78; style construction.
 
UnitDescriptor descriptor () const noexcept override
 Full descriptor — read individual fields (descriptor().id, ...) from here.
 
double numeric_as_double () const noexcept override
 Numeric value as a double (lossy for >double types).
 
std::string to_string () const override
 Render <value> <symbol> (or whatever the descriptor's formatter dictates).
 
std::string to_formatted_string () const override
 Render with the descriptor's default precision applied.
 
std::unique_ptr< IUnitValueclone () const override
 Deep copy as the interface type — the moral equivalent of a virtual copy ctor.
 

Static Public Member Functions

static std::shared_ptr< UnitValueof (T value)
 Heap-allocated shared-owned factory: auto p = MeterValue::of(1.78); For a stack value, just construct directly (MeterValue v = 1.78).
 
static std::unique_ptr< UnitValueunique (T value)
 Heap-allocated unique-owned factory: auto p = MeterValue::unique(1.78);
 
static std::expected< UnitValue, ParseErrorparse (const std::string_view input)
 Parse "42.5 m" → UnitValue<U,T>.
 

Detailed Description

template<UnitLike U, NumericValue T = double>
struct dimval::UnitValue< U, T >

A value paired at the type level with a unit tag.

Template Parameters
Ua unit struct that derives from UnitBase (see DIMVAL_DEFINE_UNIT).
Ta numeric type. Defaults to double.

Operators are restricted to same-unit + scalar to prevent silent mixing of incompatible quantities. Use dimval::convert<ToUnit>(v) to bridge between units of the same kind.

Constructor & Destructor Documentation

◆ UnitValue()

template<UnitLike U, NumericValue T = double>
constexpr dimval::UnitValue< U, T >::UnitValue ( value)
inlineconstexprnoexcept

Implicit conversion from the numeric type — enables MeterValue distance = 1.78; style construction.

Safe because the unit tag type already prevents cross-unit assignments.

Member Function Documentation

◆ parse()

template<UnitLike U, NumericValue T = double>
static std::expected< UnitValue, ParseError > dimval::UnitValue< U, T >::parse ( const std::string_view  input)
inlinestatic

Parse "42.5 m" → UnitValue<U,T>.

The trailing symbol must match the unit's id, symbol, or short_name; otherwise returns ParseErrorCode::UnitMismatch. Mirrors dimval::parse_unit_value<U,T>.


The documentation for this struct was generated from the following files: