Here's an alternative
template<typename T>
struct Type { typedef T type; };
template<typename T>
inline constexpr Type<T> type{};
template <typename P>
struct Vertex
{
static constexpr auto D3 = []{
if constexpr(std::is_same_v<P,float>)
return type<vec3>;
else if constexpr(std::is_same_v<P,double>)
return type<dvec3>;
else if constexpr(std::is_same_v<P,int>)
return type<ivec3>;
}();
static constexpr auto D2 = []{
if constexpr(std::is_same_v<P,float>)
return type<vec2>;
else if constexpr(std::is_same_v<P,double>)
return type<dvec2>;
else if constexpr(std::is_same_v<P,int>)
return type<ivec2>;
}();
typename decltype(D3)::type position;
typename decltype(D3)::type normal;
typename decltype(D2)::type texcoords;
};
With a little bit of more effort on the Type
template, you can improve the code of the lambdas quite a bit (perhaps you've seen boost hana, which follows this idea aswell)
template<typename T>
struct Type {
typedef T type;
friend constexpr bool operator==(Type, Type) {
return true;
}
};
template<typename T1, typename T2>
constexpr bool operator==(Type<T1>, Type<T2>) {
return false;
}
template<typename T>
inline constexpr Type<T> type{};
Now it will not need std::is_same_v
anymore
template <typename P>
struct Vertex
{
static constexpr auto D3 = [](auto t) {
if constexpr(t == type<float>)
return type<vec3>;
else if constexpr(t == type<double>)
return type<dvec3>;
else if constexpr(t == type<int>)
return type<ivec3>;
}(type<P>);
static constexpr auto D2 = [](auto t) {
if constexpr(t == type<float>)
return type<vec2>;
else if constexpr(t == type<double>)
return type<dvec2>;
else if constexpr(t == type<int>)
return type<ivec2>;
}(type<P>);
typename decltype(D3)::type position;
typename decltype(D3)::type normal;
typename decltype(D2)::type texcoords;
};
The ugly decltype
writing could be fixed by using auto
aswell
template<auto &t>
using type_of = typename std::remove_reference_t<decltype(t)>::type;
So you can write
type_of<D3> position;
type_of<D3> normal;
type_of<D2> texcoords;
` does exactly what you want
– Justin Oct 25 '17 at 23:48