E.V.E
v2023.02.15
 
Loading...
Searching...
No Matches
eve::struct_support< Self, Fields > Struct Template Reference

CRTP base-class to declare operators for user-defined product type. More...

Detailed Description

template<typename Self, typename ... Fields>
struct eve::struct_support< Self, Fields >

Required header: #include <eve/traits/product_type.hpp>

eve::struct_support is a CRTP based helper class to define product type like user-defined type. If Self is this type that inherits from eve::struct_supports<Self,Fields...>, it will behave as a structure containing members of type Fields... and provides supports for operators based on the implementation of Self.

Generated operators include:

  • Member-wise equality is provided by default
  • Lexicographial order is provided unless eve::supports_ordering<Self>::value evaluates to false
  • Binary operators are provided if the corresponding compound assignment operator is provided. E.g operator+ will be generated if Self::operator+= is defined. Note that no operators is generated by combining existing operator (like operator-= b from operator+= and operator-) to maximize optimization opportunities.

eve::struct_support also automatically generates the data member for Self that are stored within a kumi::tuple and accessible via get<N>(*this).

Template Parameters
SelfType to use as CRTP derived class
FieldsVariadic list describing the data member of Self

Examples

#include <eve/traits/product_type.hpp>
#include <eve/wide.hpp>
#include <iostream>
// vec3 is a product type made of 3 floats
struct vec3 : eve::struct_support<vec3, float, float, float>
{
// To simplify member access, we can wrap access to the product type
// elements via a named friend accessor.
friend decltype(auto) x(eve::like<vec3> auto&& self)
{
return get<0>(std::forward<decltype(self)>(self));
}
friend decltype(auto) y(eve::like<vec3> auto&& self)
{
return get<1>(std::forward<decltype(self)>(self));
}
friend decltype(auto) z(eve::like<vec3> auto&& self)
{
return get<2>(std::forward<decltype(self)>(self));
}
// Defines += over vec3 to automatically generates +
friend auto& operator+= ( eve::like<vec3> auto& self
, eve::like<vec3> auto const& other
)
{
x(self) += x(other);
y(self) += y(other);
z(self) += z(other);
return self;
}
// Defines -= over vec3 to automatically generates -
friend auto& operator-= ( eve::like<vec3> auto& self
, eve::like<vec3> auto const& other
)
{
x(self) -= x(other);
y(self) -= y(other);
z(self) -= z(other);
return self;
}
// Defines stream insertion
friend std::ostream& operator<<( std::ostream& os, eve::like<vec3> auto const& self )
{
return os << "{" << x(self) << ", " << y(self) << ", " << z(self) << "}";
}
};
int main()
{
eve::wide<vec3> p ( [](auto i, auto )
{
return vec3{1.f+i,4.f-i,1.f/(1+i)};
}
);
std::cout << p << "\n";
p = p + p;
std::cout << p << "\n";
p -= vec3{1,1,1};
std::cout << p << "\n";
}
Specifies semantic compatibility between wrapper/wrapped types.
Definition product_type.hpp:107
CRTP base-class to declare operators for user-defined product type.
Definition product_type.hpp:154
Wrapper for SIMD registers.
Definition wide.hpp:70