Page MenuHome
Paste P1433

(An Untitled Masterwork)
ActivePublic

Authored by Jacques Lucke (JacquesLucke) on May 28 2020, 1:08 PM.
#include <iostream>
struct float3 {
typedef float base;
float x, y, z;
friend float3 operator+(const float3 &a, const float3 &b) {
return {a.x + b.x, a.y + b.y, a.z + b.z};
}
};
struct double3 {
typedef double base;
double x, y, z;
friend double3 operator+(const double3 &a, const double3 &b) {
return {a.x + b.x, a.y + b.y, a.z + b.z};
}
};
template <typename T> struct vec3_impl;
template <> struct vec3_impl<float> { typedef float3 type; };
template <> struct vec3_impl<double> { typedef double3 type; };
template <typename T> using vec3 = typename vec3_impl<T>::type;
template <typename T> vec3<T> some_operation(vec3<T> value) {
return value + vec3<T>{5, 5, 5};
}
template <typename T> T another_operation(vec3<T> a, vec3<T> b) {
vec3<T> c = a + some_operation<T>(b);
T value = a.x + b.x;
return value + c.x;
}
int main(int argc, char const *argv[]) {
{
float3 a = {1, 1, 1};
float3 b = {2, 2, 2};
float result = another_operation<float3::base>(a, b);
std::cout << "float: " << result << "\n";
}
{
double3 a = {1, 1, 1};
double3 b = {2, 2, 2};
double result = another_operation<decltype(a)::base>(a, b);
std::cout << "double: " << result << "\n";
}
return 0;
}