【发布时间】:2020-12-13 02:51:14
【问题描述】:
我正在考虑在我的程序中使用 color spaces 结构。现在您可以看到每个色彩空间对其每个组件都有其限制。所以考虑这样的结构。
template<typename T>
struct SomeColorSpace{
T component1,component2,.....; // or std::array<T,N> components, N is compile-time const
};
现在我需要以某种方式定义一个范围,以便创建一个结构。
template<typename T>
struct Range{
T min, max;
// Some constructors initializing min and max...
};
现在我需要我的颜色空间结构来了解其每个组件的范围。但正如你所见,我不能只在我的结构中保留std::array<Ranges<T>,N> ranges,因为范围因类型而异。例如,考虑struct RGB,每个组件可能是float 或unsigned char,因此当组件为float 时,每个组件的范围变为[0,1];,当它是无符号字符时,它是[0,255];。为每种情况编写模板专业化是一种解决方案,但我想要这样的签名。 using RGBFloat = RGB<float,Range<float>...>。我的意思是我想通过模板参数包传递范围。我知道例如Range<float>(0,1); 是一个非类型模板参数,所以一种解决方法是像这样重构struct Range
template<typename T,T _MIN, T _MAX>
struct Range{
static constexpr T MIN = _MIN;
static constexpr T MAX = _MAX;
};
所以我可以将Range<T,T min, T max> 作为模板参数包传递,但我必须将包保存在std::tuple 中(我也不想要这个)。我的问题是您是否通过重构 Range 结构或其他方式看到任何其他可能性以具有定义颜色空间的签名。 using DefineColorSpace = ColorSpace<unsigned char, ranges....>。我知道C++ 20 对非类型模板参数进行了巨大的重构,但我正在使用clang 并且似乎他们do not support 还具有该功能。
任何建议都会有所帮助,谢谢)
【问题讨论】:
标签: c++ templates range variadic-templates parameter-pack