Hey guys, let me first start off by describing my high-level design goals.
I'm writing some numeric code and I want to avoid things like macros and global typedefs so I'm going with templates. This code is sort of library-oriented so I'm trying to make it easy for the caller to change things.
CUDA has some pretty sexy built-in "vector types". They are, int2, int3, int4, float2, float3, float4, double2, etc.
These are basic POD-types but they have stringent alignment requirements which allows the GPU to optimize read instructions (they have x, y, z and w data members accordingly).
So, I'm trying to write a function that returns either a vector a float3 or double3 but maintains the same calling convention and implementation. This is the actual goal of the code so far.
Here's my attempt (with help from google):
Code:
#include <type_traits>
template <bool B, class T = void>
using enable_if_t = typename std::enable_if<B, T>::type;
template <
typename T, enable_if_t<std::is_floating_point<T>::value>>
struct point_struct
{
typedef T type;
};
template <>
struct point_struct<float>
{
typedef float3 type;
};
template <>
struct point_struct<double>
{
typedef double3 type;
};
// function I'm writing
template <typename T>
auto gen_cartesian_domain(int const gl) -> thrust::device_vector<typename point_struct<T>::type>
{
// right now it just default constructs
return thrust::device_vector<typename point_struct<T>::type>{};
}
So, I'm getting the error: "error: too few arguments for class template "point_struct""
This only happens if I use std::enable_if. It's worth noting that the code works without the enable_if call. But I want the enable_if call because I only want this type to be visible for floats and doubles.
I also really don't understand the error I'm getting from the compiler. I kind of just assumed that the enable_if call would still be evaluated, even in the case of specialization.
Is there perhaps a better way of achieving my goal? And why am I specializing this class incorrectly? I don't mind switching up how I'm doing things but I'd first like to understand why it is that I'm wrong in the first place.