模板参数是否可以无缝匹配类型和模板,也许是可变变量的一部分
Can template parameters match both types and templates seamlessly, perhaps as part of a variadic?
我的代码使用类似于Boost.PolyCollection
中的静态类型结构存储一些状态。
我认为,我的问题是下面的代码至少说明了问题。基本上,我正在使用参数包,并且需要一种方法来通过包中的内容"实例化"给定的模板。
#include <unordered_map>
template<typename... Ts>
struct Pack
{
/*
instantiate a given template with passed types + the types in this Pack
but passed Template may take non-type template parameters, what to do??
*/
// template<template<typename...> class Template, typename... As> // error: type/value mismatch at argument 1 in template parameter list for 'template<class ... Ts> template<template<template<class ...> class Template, class ... As> template<class ... Ts> template<class ...> class Template, class ... As> using Inst = Template<As ..., Ts ...>'
// using Inst = Template<As..., Ts...>;
// this works for my case, but it's too specific and ugly -
// am fixing the first of As to be a non-type
template<template<template<typename...> class, typename...> class Template, template<typename...> class A1, typename... As>
using Inst = Template<A1, As..., Ts...>;
};
template<template<typename...> class Segment, typename Key, typename... Ts>
class AnyMap
{
};
int main()
{
typedef Pack<int, char> ServicePack;
typedef long Key;
using ServiceMap = typename ServicePack::template Inst<AnyMap, std::unordered_map, Key>; // AnyMap with given segment type and key
}
我希望auto...
,我没有用太多,来救援,但似乎auto
模板模板参数不匹配,它仅适用于推断类型的值。
您知道实现这一目标的简单方法吗?
(也许很明显,这是关于 C++17)
有两种相关的方法。
首先是助推哈纳风格。 将所有内容转换为编译时值。 模板? 一个值。 类型? 一个值。 值? 类似整数常量的类型的实例。
元编程现在是constexpr编程。
第二种方法是将所有内容转换为类型。
这包括模板的非类型模板参数。
template<claas T, class N>
using array=std::array<T,N{}()>;
template<auto x>
using k=std::integral_constant<decltype(x), x>;
现在我们可以将 k<77>
作为表示非类型模板参数的类型传递给 77
array<int,k<77>>
并获取 std::array<int,77>
.
仅类型的数组模板现在很容易进行元编程。 只需编写一次这些包装器,然后元程序即可。
然后,传递模板可以是:
template<template<class...>class> struct Z{};
现在我们可以将Z<array>
作为类型传递。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 将数组的地址分配给变量并删除
- 为"adjacent"变量赋值时出现问题
- enum是C++中的宏变量还是整数变量
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 用C++中的一个变量定义一个常量
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 你能重载对象变量名本身返回的内容吗
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 尝试通过多个向量访问变量时,向量下标超出范围
- 试图让变量检查数组中的某些内容
- Cpp-Tuple使用带有变量的get
- 将包含C样式数组的对象初始化为成员变量(C++)
- 当vector是tje全局变量时,c++中vector的内存管理
- 通过多个头文件使用常量变量
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 模板参数是否可以无缝匹配类型和模板,也许是可变变量的一部分
- 告诉子类对超类的受保护变量执行某些操作是否是一种好的做法(也许是一些已知的设计模式?)?