Thread: Specializing classes with enable_if

  1. #1
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665

    Specializing classes with enable_if

    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.

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Okay, I did it! I'm officially the King of Templated Code! Lol jk. _But_ I did get it work so I'm pretty stoked!

    Code:
    // Example program
    #include <iostream>
    #include <string>
    #include <type_traits>
    
    
    struct float3
    {
        float x;
        float y;
        float z;
    };
    
    
    template <
        typename T,
        typename = std::enable_if_t<std::is_floating_point<T>::value>>
    struct point_struct
    {
        typedef T type;    
    };
    
    
    template <>
    struct point_struct<float>
    {
        typedef float3 type;    
    };
    
    
    int main(void)
    {
        auto my_floats = point_struct<float>::type{1.0, 2.0, 3.0};
        std::cout << my_floats.x << " " << my_floats.y << " " << my_floats.z << std::endl;
        return 0;
    }
    Output:
    Code:
    1 2 3
    Here's a link: C++ Shell
    Last edited by MutantJohn; 06-30-2016 at 11:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to achieve generic code while specializing behavior?
    By MutantJohn in forum C++ Programming
    Replies: 14
    Last Post: 01-04-2016, 08:16 PM
  2. Specializing a variadic template: C++ Primer ex. 16.64
    By frank67 in forum C++ Programming
    Replies: 1
    Last Post: 10-31-2015, 12:46 PM
  3. Replies: 9
    Last Post: 04-06-2011, 01:37 PM
  4. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  5. Problem with specializing templates
    By anon in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2008, 11:34 AM

Tags for this Thread