简体   繁体   中英

POD Template class with optional members

I am looking for a way to optionally include members of a c++ class to generate POD structs. I found that this works fairly well, but is nonstandard:

#include <iostream>

template <int v, int n, int t>
struct Point
{
    int vertex[v];
    float normal[n];
    double texcoord[t];
};

int main()
{
    std::cout << (sizeof (Point<0,0,1>)) << std::endl;
    std::cout << (sizeof (Point<1,0,1>)) << std::endl;
    std::cout << (sizeof (Point<1,1,2>)) << std::endl;
    std::cout << (sizeof (Point<0,0,0>)) << std::endl;
    return 0;
}

So Point<1,0,0> would only contain a vertex (the int type will actually be a vector3 type in practice), and so on. The main reason for this is to easily support interleaved arrays for OpenGL.

Maybe try something like this:

#include <type_traits>

template <unsigned int v, unsigned int n, unsigned int t>
struct Point
{
    int data[v + n + t];

    template <unsigned int i>
    typename std::enable_if<(i < v), int &>::type
    vertex() { return data[i]; }

    template <unsigned int i>
    typename std::enable_if<(i < v + n), int &>::type
    normal() { return data[v + i]; }

    template <unsigned int i>
    typename std::enable_if<(i < v + n + t), int &>::type
    texcoord() { return data[v + n + i]; }
};

Usage:

Point<1,1,2> p;
p.vertex<0>() = 50;

std::array<T, 0> is valid, unlike T[0] and really is a better solution all around. Unfortunately, and since this is only available for C++11, I can't figure out if its Boost namesake boost::array also has such support.

It is also possible to write an array -like helper yourself.

You can do this, but you would need specializations.

Edit: Matched with new question requirement of different type per array. Made template arguments unsigned though.

template <> struct Point<0, 0, 0> {};
template <unsigned v> struct Point<v, 0, 0> { int vertex[v]; };
template <unsigned n> struct Point<0, n, 0> { float normal[n]; };
template <unsigned t> struct Point<0, 0, t> { double texcoord[t]; };
template <unsigned v, unsigned n> struct Point<v, n, 0> {
    int vertex[v];
    float normal[n];
};
template <unsigned v, unsigned t> struct Point<v, 0, t> {
    int vertex[v];
    double texcoord[t];
};
template <unsigned n, unsigned t> struct Point<0, n, t> {
    float normal[n];
    double texcoord[t];
};

Each specialization leaves out the associated array when its corresponding template parameter has value 0 . For three items as shown, the enumeration of specializations is not overly large, and provides exactly the same syntax for accessing the elements as your original post but using standard C++ constructs.

Extending this technique beyond 3 arrays would be cumbersome. However, it could be managed with a script to generate the specializations for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM