DGtal  1.5.beta
DGtal::ArithmeticConversionTraits< T, U, Enable > Struct Template Reference

Aim: Trait class to get result type of arithmetic binary operators between two given types. More...

#include <DGtal/kernel/ArithmeticConversionTraits.h>

Inheritance diagram for DGtal::ArithmeticConversionTraits< T, U, Enable >:
[legend]

Detailed Description

template<typename T, typename U, typename Enable = void>
struct DGtal::ArithmeticConversionTraits< T, U, Enable >

Aim: Trait class to get result type of arithmetic binary operators between two given types.

Template Parameters
TFirst operand type.
USecond operand type.
EnableInternal type used for SFINAE (do not specify it).

If arithmetic operations are valid between T and U types, it provides a type typedef that is defined to the result type of such operations.

Otherwise, there is no type member.

The idea is to be able to use binary operators (specifically the arithmetic operators) with two different operand types and to identify the best result type.

For arithmetic types (int, double, ...), this trait class relies on std::common_type standard type trait that use internal conversion rules applied with ternary operator, i.e. the type of false ? std::declval<T>() : std::declval<U>(). For arithmetic types, it is equivalent to the type of T() + U(). For example, those rules result in the following conversion chain: int8_t -> int16_t -> int32_t -> int64_t -> float -> double.

However, this class is not equivalent to std::common_type that defines the common type that U and T can be implicitly converted to. For example, a PointVector added with an integer will result in a PointVector but there is no implicit conversion from an integer to a PointVector (and if there was such conversion, it may not lead to the same result).

This class is meant to be specialized when needed, provided that (similarly to the specialization conditions of std::common_type):

  • T or U are user-defined types,
  • T and U used for the specialization aren't references, neither const.
See also
https://en.cppreference.com/w/cpp/types/common_type
https://en.cppreference.com/w/cpp/language/operator_arithmetic#Conversions

Definition at line 92 of file ArithmeticConversionTraits.h.


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