将数组与可变模板相关联
Associating an array with a variadic template
我现在正在学习一些关于C++11、C++14和C++1z中的模板和模板的知识。我正在尝试编写一个带有内部类的可变类模板,该类将把int
与每个模板参数关联起来,并有一个返回其数组表示的constexpr
方法。
假设我已经确保模板不能接收两个相同类型的参数。我想这样做:
template <typename... Types>
struct MyVariadicTemplate {
//we know that all types in Types... are different
template <int... Values>
struct MyInnerTemplate {
//I need to make sure that sizeof...(Values) == sizeof...(Types)
constexpr std::array<int, sizeof...(Values)> to_array() {
std::array<int, sizeof...(Values)> result = {Values...};
return result;
// this is only valid since C++14, as far as I know
}
};
};
这个代码应该是有效的(如果不是,我很想知道为什么)。现在,我想添加另一个内部模板:
template <typedef Type>
struct AnotherInnerTemplate {};
它有一个public typedef
,它代表MyInnerTemplate
,在Types...
中Type
的位置上有一个,在其他地方有零——在这里我迷路了。我不知道如何进行
如果我朝着错误的方向前进,我希望有人能给我一个如何做到这一点的提示。
我想你想要的是这样的东西。
#include <array>
#include <cstddef>
#include <iostream>
#include <type_traits>
template <typename NeedleT, typename... HaystackTs>
constexpr auto get_type_index_mask() noexcept
{
constexpr auto N = sizeof...(HaystackTs);
return std::array<bool, N> {
(std::is_same<NeedleT, HaystackTs>::value)...
};
}
template <typename T, std::size_t N>
constexpr std::size_t ffs(const std::array<T, N>& array) noexcept
{
for (auto i = std::size_t {}; i < N; ++i)
{
if (array[i])
return i;
}
return N;
}
int
main()
{
const auto mask = get_type_index_mask<float, bool, int, float, double, char>();
for (const auto& bit : mask)
std::cout << bit;
std::cout << "n";
std::cout << "float has index " << ffs(mask) << "n";
}
输出:
00100
float has index 2
神奇的发生在参数包扩展
(std::is_same<NeedleT, HaystackTs>::value)...
在HaystackTs
中的每种类型与NeedleT
进行测试。如果您想将const int
和int
视为同一类型,则可能需要将std::decay
应用于任一类型。
template <int size, int... Values> struct AnotherImpl {
using Type = typename AnotherImpl<size - 1, Values..., 0>::Type;
};
template <int... Values> struct AnotherImpl<0, Values...> {
using Type = Inner<Values...>;
};
template <class T> struct Another {
using Type = typename AnotherImpl<sizeof...(Types) - 1, 1>::Type;
};
完整:
template <class... Types> struct My {
template <int... Values> struct Inner {
constexpr std::array<int, sizeof...(Values)> to_array() {
return std::array<int, sizeof...(Values)>{Values...};
}
};
template <int size, int... Values> struct AnotherImpl {
using Type = typename AnotherImpl<size - 1, Values..., 0>::Type;
};
template <int... Values> struct AnotherImpl<0, Values...> {
using Type = Inner<Values...>;
};
template <class T> struct Another {
using Type = typename AnotherImpl<sizeof...(Types) - 1, 1>::Type;
};
};
auto main() -> int {
My<int, float, char>::Another<int>::Type s;
auto a = s.to_array();
for (auto e : a) {
cout << e << " ";
}
cout << endl;
return 0;
}
打印:
1 0 0
这是你想要的吗?
相关文章:
- 查找自动生成键并具有线性内存消耗的小型关联数组
- 创建一个结构的关联数组,以创建一个缓冲区,供键快速访问
- 如何使用 PHPCPP 传递对象数组,遍历每个对象并返回关联数组
- 如何实现轻量级快速关联数组
- 如何在C++中实现多维关联数组
- C++ - 使用关联数组,按值排序后无法按键搜索
- STL中关联数组(贴图)的速度
- Qt JSON 编码关联数组和对象
- 将数组与可变模板相关联
- C++相当于Perl的Tie::IxHash(索引关联数组)?
- C++交换成员数组和关联的指针
- 对字符串数组的关联
- 为什么只有非常量映射提供类似于关联数组的直接元素检索?
- c++ 0x 3d map初始化类似于php的关联数组
- 这段代码是在c++中访问类中的关联数组吗?
- 我怎么能有一个关联数组与许多值
- 如何从一个Node.js c++插件传递一个关联数组到js代码
- 关联数组的含义是什么?
- 具有用户指定维度的数组(或关联数组)
- 创建集合关联数组