Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 结构化模板参数_C++_Templates_C++20 - Fatal编程技术网

C++ 结构化模板参数

C++ 结构化模板参数,c++,templates,c++20,C++,Templates,C++20,在这个答案的基础上: 我想将模板参数构造成模式,因为相同的类在同一应用程序的不同上下文中使用 // adapted from https://stackoverflow.com/a/3319851 template<typename _string, typename _real> struct scheme{ public: using string = _string; using real = _real; }; using scheme1 = sch

在这个答案的基础上:

我想将模板参数构造成模式,因为相同的类在同一应用程序的不同上下文中使用

// 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::数组进行同样的参数化

编辑:添加更多信息

我想实现这样的目标:

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不是有效的类型名。但是我需要能够仅在模板实例化的地方提供这些最终参数,例如在测试类中

我为什么想要这样的东西

显然,有些类既可以在constexpr方案中使用所有const数据,也可以在各种非const方案中使用。例如,对于constexpr方案,我可以使用std::string_视图,对于非const方案,我可以使用std::string视图

另一个例子是提供与特定版本二进制数据方案中持久化的数据的兼容性,将数据从32位迁移到64位,将代码页迁移到Unicode等。。较新版本的应用程序需要实现所有方案,才能在升级后读取旧数据

谢谢, 标记

您需要将_集合设置为模板参数

template<typename _string, typename _real, template<typename, size_t> typename _collection>
struct scheme{
    public:
    using string = _string;
    using real = _real;
    // This is now a template type alias, i.e. "variable inside the scheme"
    template<typename T, size_t N>
    using collection = _collection<T, N>;
};
请注意,此解决方案仅适用于具有与std::array相同签名的模板,即一个类型参数后跟一个整型非类型参数。例如,std::vector将不起作用。 因此,根据myNamespace::myArray的实际类型,此解决方案可能无法解决您的问题

编辑:我意识到你在问题中添加了c++20标记。在这种情况下,您可以省略测试类中的三个类型名。 实时代码。

您需要将_集合设置为模板参数

template<typename _string, typename _real, template<typename, size_t> typename _collection>
struct scheme{
    public:
    using string = _string;
    using real = _real;
    // This is now a template type alias, i.e. "variable inside the scheme"
    template<typename T, size_t N>
    using collection = _collection<T, N>;
};
请注意,此解决方案仅适用于具有与std::array相同签名的模板,即一个类型参数后跟一个整型非类型参数。例如,std::vector将不起作用。 因此,根据myNamespace::myArray的实际类型,此解决方案可能无法解决您的问题

编辑:我意识到你在问题中添加了c++20标记。在这种情况下,您可以省略测试类中的三个类型名。
实时代码。

我不清楚您要解决的编程问题。你可能想在你的帖子中添加更多细节。你到底想实现什么?您可以添加其他模板参数,以便添加要测试的参数,特别是对于std::array。您的方案可能具有int或std::array的类型,constepr size_t为10,或者甚至是使用int,10代替std::array的模板。您需要如何以及在何处进行参数化?这是因为类测试需要多种类型的列表,这些列表可能是多种相同大小的std::数组?为什么当前模式不起作用?我想提供帮助,但我很难理解您实际需要什么。我想到了模板参数,但没有提供足够的信息来完全理解它是否是您所需要的。我不清楚您试图解决的编程问题。你可能想在你的帖子中添加更多细节。你到底想实现什么?您可以添加其他模板参数,以便添加要测试的参数,特别是对于std::array。您的方案可能具有int或std::array的类型,constepr size_t为10,或者甚至是使用int,10代替std::array的模板。您需要如何以及在何处进行参数化?这是因为类测试需要多种类型的列表,这些列表可能是多种相同大小的std::数组?为什么当前模式不起作用?我想提供帮助,但我很难理解您实际需要什么。我想到了模板参数,但没有提供足够的信息来完全理解它是否是您所需要的。非常感谢,这很好。你能推荐一本关于这些事情的书吗?介绍C++20?不幸的是没有,我是通过反复试验痛苦地学习的。非常感谢,这很好。你能推荐一本关于这些事情的书吗?介绍C++20?不幸的是没有,我通过反复试验痛苦地学习了它。