SOFA API  cbddc29e
Open source framework for multi-physics simuation
sofa::type::StrongType< TUnderlyingType, UniqueIdentifyingTag, Functionalities > Struct Template Reference

#include <StrongType.h>

Inheritance diagram for sofa::type::StrongType< TUnderlyingType, UniqueIdentifyingTag, Functionalities >:

Detailed Description

template<class TUnderlyingType, class UniqueIdentifyingTag, template< typename > class... Functionalities>
struct sofa::type::StrongType< TUnderlyingType, UniqueIdentifyingTag, Functionalities >

A template class to create strong, type-safe aliases for existing types, with optional extensible functionalities.

The StrongType class is designed to add type safety by creating wrapper types around primitive types or existing classes. By defining unique, named types for different uses of the same underlying type, StrongType helps prevent mistakes caused by passing incorrect parameters of the same type.

For example, instead of passing two int parameters to a function (e.g., for "width" and "height"), StrongType allows you to define distinct types for width and height:

using Width = StrongType<int, struct WidthTag>;
using Height = StrongType<int, struct HeightTag>;
void resize(Width width, Height height);
resize(Width{5}, Height{10}); // Correct
resize(Height{10}, Width{5}); // Compilation error

Strongly inspired by https://github.com/joboccara/NamedType

Template Parameters
TUnderlyingTypeThe type to be wrapped by StrongType
UniqueIdentifyingTagA unique tag type to create a distinct StrongType from other wrappers around the same TUnderlyingType.
FunctionalitiesA set of optional functionalities to extend StrongType

Public Member Functions

constexpr StrongType (const UnderlyingType &v)
 
constexpr UnderlyingTypeget () noexcept
 
constexpr const std::remove_reference_t< UnderlyingType > & get () const
 

Constructor details

◆ StrongType()

template<class TUnderlyingType , class UniqueIdentifyingTag , template< typename > class... Functionalities>
constexpr sofa::type::StrongType< TUnderlyingType, UniqueIdentifyingTag, Functionalities >::StrongType ( const UnderlyingType v)
inlineexplicitconstexpr

Function details

◆ get() [1/2]

template<class TUnderlyingType , class UniqueIdentifyingTag , template< typename > class... Functionalities>
constexpr const std::remove_reference_t<UnderlyingType>& sofa::type::StrongType< TUnderlyingType, UniqueIdentifyingTag, Functionalities >::get ( ) const
inlineconstexpr

◆ get() [2/2]

template<class TUnderlyingType , class UniqueIdentifyingTag , template< typename > class... Functionalities>
constexpr UnderlyingType& sofa::type::StrongType< TUnderlyingType, UniqueIdentifyingTag, Functionalities >::get ( )
inlineconstexprnoexcept