在 C++11 中将静态 constexpr 数组转换为模板参数
Convert static constexpr array to template parameter in C++11
假设我们有一个constexpr
数组,如下所示:
static constexpr unsigned int a[] = {2, 8, ... ,6}; // N element
我想将此数组用作模板参数包:
typedef SomeTemplatedStruct<a[0], a[1], ... ,a[N - 1]> tmp;
正如此处所述,可以在 C++14 中执行此操作。但是,我未能将该代码转换为 C++11。任何帮助,不胜感激。
如果你已经打算自己动手,就没有必要实现一个整数序列辅助程序,因为我们可以使用已经扩展的包的大小作为索引,以便在递归期间从数组中取出下一个元素:
template <typename A, A& a, typename U = typename std::remove_reference<decltype(a[0])>::type, bool = true, U... unpack>
struct unravel;
template <typename T, int N, T (&a)[N], typename U, U... unpack>
struct unravel<T[N], a, U, false, unpack...>
{
template <template <U...> class Thingie>
using into = Thingie<unpack...>;
};
template <typename T, int N, T (&a)[N], typename U, U... unpack>
struct unravel<T[N], a, U, true, unpack...> : unravel<T[N], a, U, (sizeof...(unpack) + 1 < N), unpack..., a[sizeof...(unpack)]> {};
用法:
struct Blub
{
static constexpr unsigned int a[] = {2, 8, 5, 7, 6};
};
template <unsigned int...>
struct TheThingToMake {};
void test()
{
typename unravel<decltype(Blub::a), Blub::a>::into<TheThingToMake> blub;
}
现场示例
注意:这不适用于大小为 0 的数组,但这些是非标准的,我想无论如何都不是一个有趣的用例......
我能想象到的最好的办法是开发一个C++11的替代品来代替std::index_sequence
和std::make_index_sequence
,并应用你链接的C++14解决方案。
但是如果你避免它,你可以使用递归。
给出一个自制的整数序列
template <typename T, T...>
struct myIntegerSequence
{ };
和帮助程序结构mot_h
("制作输出模板帮助程序"(
template <typename T, T, std::size_t, typename>
struct mot_h;
// recursive version
template <typename T, std::size_t N, const T(&A)[N], std::size_t Pos, T ... ts>
struct mot_h<const T(&)[N], A, Pos, myIntegerSequence<T, ts...>>
: mot_h<const T(&)[N], A, Pos+1u, myIntegerSequence<T, ts..., A[Pos]>>
{ };
// ground case
template <typename T, std::size_t N, const T(&A)[N], T ... ts>
struct mot_h<const T(&)[N], A, N, myIntegerSequence<T, ts...>>
{ using type = myIntegerSequence<T, ts...>; };
您可以编写以下模板output
template <typename T, T inp>
using output = typename mot_h<T, inp, 0u,
myIntegerSequence<
typename std::remove_const<
typename std::remove_reference<decltype(inp[0])>::type>::type>
>::type;
以下是完整的编译C++11示例
#include <type_traits>
template <typename T, T...>
struct myIntegerSequence
{ };
constexpr int input[] = { 2, 3, 5, 7, 11, 13, 17, 19 };
template <std::size_t N, typename T, const T (&A)[N]>
struct foo
{ };
template <typename T, T, std::size_t, typename>
struct mot_h;
template <typename T, std::size_t N, const T(&A)[N], std::size_t Pos, T ... ts>
struct mot_h<const T(&)[N], A, Pos, myIntegerSequence<T, ts...>>
: mot_h<const T(&)[N], A, Pos+1u, myIntegerSequence<T, ts..., A[Pos]>>
{ };
template <typename T, std::size_t N, const T(&A)[N], T ... ts>
struct mot_h<const T(&)[N], A, N, myIntegerSequence<T, ts...>>
{ using type = myIntegerSequence<T, ts...>; };
template <typename T, T inp>
using output = typename mot_h<T, inp, 0u,
myIntegerSequence<
typename std::remove_const<
typename std::remove_reference<decltype(inp[0])>::type>::type>
>::type;
int main ()
{
using target = myIntegerSequence<int, 2, 3, 5, 7, 11, 13, 17, 19>;
static_assert( std::is_same<output<decltype((input)), input>,
target>::value, "!" );
}
相关文章:
- C++转换参数初始化问题
- C2664 无法从'initializer list'转换参数
- 隐式可转换参数,但属于引用类型
- 传递到函数(C )时转换参数
- 了解错误 C2664:无法转换参数 1
- 错误 C2664:无法从'NvPhysicalGpuHandle'转换参数 1
- Unicode 字符问题/转换参数
- 在运行时根据某些元数据强制转换参数包值
- 错误 C2664:"errno_t wcstombs_s(size_t *,字符 *,size_t,常量 wchar_t *,size_t)":无法转换参数 4
- 错误C2664:无法转换参数错误
- 无法转换参数错误
- Boost.python自动转换参数
- "尝试升级使用已弃用的转换参数指定的输入文件"是什么意思?
- 是否可以在函数模板中转换参数C++?
- 错误C2664:无法转换参数(指纹传感器检测)
- 无法转换参数 - 模板错误
- 编译zxing时,Libconv无法转换参数
- C++ 无法转换参数
- std::p air<_Ty1,_Ty2>::p air<_Ty1,_Ty2>& 无法转换参数
- 无法转换参数-转换丢失限定符