【发布时间】:2021-01-22 15:24:40
【问题描述】:
建立在这个答案之上:
https://stackoverflow.com/a/3319851/13250135
我想将我的模板参数结构化为方案,因为在同一应用程序的不同上下文中使用相同的类。
// adapted from https://stackoverflow.com/a/3319851
template<typename _string, typename _real>
struct scheme{
public:
using string = _string;
using real = _real;
};
using scheme1 = scheme<std::string_view, double>;
using scheme2 = scheme<std::string, float>;
template<class scheme>
class Test {
scheme::string text;
scheme::real value;
std::array<int, 10> array{};
};
Test<scheme1> test1{};
Test<scheme2> test2{};
到目前为止一切顺利。但现在我需要一种本身就是参数的类型。例如,我想用潜在的替代方案对std::array<T> 进行同样的参数化。
编辑:添加更多信息。
我想实现这样的目标:
template<typename _string, typename _real, typename _collection>
struct scheme{
public:
using string = _string;
using real = _real;
using collection = _collection;
};
using scheme1 = scheme<std::string_view, double, std::array>;
using scheme2 = scheme<std::string, float, myNamespace::myArray>;
template<class scheme>
class Test {
scheme::string text;
scheme::real value;
scheme::collection<int, 10> array{};
};
但这当然行不通,因为没有参数的std::array 不是有效的类型名。但是我需要能够仅在模板实例化的地方提供这些最终参数,例如在Test 类中。
我为什么想要这样的东西?
有些类既可用于constexpr 方案(显然是所有 const 数据),也可用于各种非 const 方案。例如,我可以将std::string_view 用于constexpr 方案,将std::string 用于非常量方案。
另一个示例可能是提供与在特定于版本的二进制数据方案中持久保存的数据的兼容性(将数据从 32 位迁移到 64 位,代码页到 Unicode 等)。较新版本的应用程序需要实现所有方案才能在升级后读取旧数据。
谢谢, 标记
【问题讨论】:
-
我不清楚您要解决什么编程问题。您可能想edit您的帖子并添加更多详细信息。
-
你到底想达到什么目的?可以添加额外的模板参数,这样就可以给
Test添加参数,专门针对std::array。 -
你的
scheme可能有int或std::array<int, 10>的类型,constexpr size_t的10,甚至是template using以int, 10代替std::array。 -
您需要如何以及在何处进行参数化?是
Test类需要多种列表,可能是相同大小的多种std::array?为什么当前模式不起作用?我想提供帮助,但我很难理解您真正需要什么。 -
template template参数浮现在脑海中,但没有提供足够的信息来完全了解它是否是您需要的。