基于特征存在的演绎类型

时间:2015-07-01 06:24:42

标签: c++ typetraits type-deduction

以下repo试图获取std :: tuple并迭代它以输出与之关联的各种值。 std :: tuple是一个顶点,最后使用它将在元素上调用glEnableVertexArray和glVertexAttribPointer。

到目前为止,我已经迭代了元组的组件类型,并在每个元素的每个元组中找到偏移量。但是我对这个功能感到困惑:

template<class T>
void EmitAttribute(T const & v, int stride, int offset, int i)
{
    std::cout << "Stride is " 
              << stride 
              << " element index " 
              << i 
              << " is at offset " 
              << offset 
              << " has 1 component " 
              << std::endl;
}

对于基本类型(非结构),我想发出&#34;有一个组件&#34;。对于具有num_components特征的元素,我想要发出组件数。我试过了:

template<class T, class S>
void EmitAttribute(T<S> const & v, int stride, int offset, int i)
{
    ...
              << " has " << T::num_components << " components " 
    ...     
}

但它没有编译。我如何编写模板,以便在T没有num_components特征时调用一个函数而另一个函数在调用时调用?

完整回购:

#include <iostream>
#include <tuple>

template<class T, int C>
struct vec
{
    typedef T value_type;

    enum { num_components = C };
};

template<class T>
struct vec2 : vec<T, 2>
{
public:

    T x, y;
    vec2(T X, T Y) : x(X), y(Y) {}
};

template<class T>
struct vec3 : vec<T, 3>
{
public:

    T x, y, z;
    vec3(T X, T Y, T Z) : x(X), y(Y), z(Z) {}
};

template<class T>
struct vec4 : vec<T, 4>
{
public:

    T x, y, z, w;
    vec4(T X, T Y, T Z, T W) : x(X), y(Y), z(Z), w(W) {}
};

namespace VertexAttributes
{
    template<class T>
    void EmitAttribute(T const & v, int stride, int offset, int i)
    {
        std::cout << "Stride is " 
                  << stride 
                  << " element index " 
                  << i 
                  << " is at offset " 
                  << offset 
                  << " has 1 component " 
                  << std::endl;
    }

    template<int index, class T>
    int ElementOffset(T & t)
    {
        return static_cast<int>(reinterpret_cast<char*>(&std::get<index>(t)) - reinterpret_cast<char*>(&t));
    }

    template<int index, typename... Ts>
    struct Emitter {
        void EmitAttributes(std::tuple<Ts...>& t, unsigned size) {
            EmitAttribute(std::get<index>(t), size, ElementOffset<index>(t), index);
            Emitter <index - 1, Ts...> {}.EmitAttributes(t, size);
        }
    };

    template<typename... Ts>
    struct Emitter < 0, Ts... > {
        void EmitAttributes(std::tuple<Ts...>& t, unsigned size) {
            EmitAttribute(std::get<0>(t), size, ElementOffset<0>(t), 0);
        }
    };

    template<typename... Ts>
    void EmitAttributes(std::tuple<Ts...>& t) {
        auto const size = std::tuple_size<std::tuple<Ts...>>::value;
        Emitter < size - 1, Ts... > {}.EmitAttributes(t, sizeof(std::tuple<Ts...>));
    }
}

int main()
{
    typedef std::tuple<vec2<float>, vec3<double>, vec4<float>> vertexf;
    typedef std::tuple<vec2<double>, vec3<float>, vec4<double>> vertexd;
    typedef std::tuple<int, vec3<unsigned>, double> vertexr;

    vertexf vf = std::make_tuple(vec2<float>(10, 20), vec3<double>(30, 40, 50), vec4<float>(60, 70, 80, 90));
    vertexd vd = std::make_tuple(vec2<double>(10, 20), vec3<float>(30, 40, 50), vec4<double>(60, 70, 80, 90));
    vertexr vr = std::make_tuple(100, vec3<unsigned>(110, 120, 130), 140.5);

    VertexAttributes::EmitAttributes(vf);
    VertexAttributes::EmitAttributes(vd);
    VertexAttributes::EmitAttributes(vr);

    return 0;
}

1 个答案:

答案 0 :(得分:1)

您可以创建特征

namespace detail
{
    template <typename T>
    decltype(T::num_components, void(), std::true_type{}) has_num_components_impl(int);

    template <typename T>
    std::false_type has_num_components_impl(...);
}

template <typename T>
using has_num_components = decltype(detail::has_num_components_impl<T>(0));

然后使用SFINAE或标签调度:

template <typename T>
std::enable_if_t<!has_num_components<T>::value, std::size_t>
get_num_components() { return 1; }

template <typename T>
std::enable_if_t<has_num_components<T>::value, std::size_t>
get_num_components() { return T::num_components; }

最后:

template<class T>
void EmitAttribute(T const & v, int stride, int offset, int i)
{
    std::cout << "Stride is "
              << stride
              << " element index "
              << i
              << " is at offset "
              << offset
              << " has "
              << get_num_components<T>()
              << " component "
              << std::endl;
}

Live Demo