templates 相关问题

模板标签用于多种上下文:通用编程(尤其是C ++),以及使用模板引擎生成数据/文档。在实现繁重的问题上使用此标记时 - 标记实现编写的代码语言。

使用模板来防止缩小范围但允许扩大输入范围

我有一个类模板,旨在在浮点上进行模板化。 该类有一个方法,应该允许在需要时扩大输入(例如 float -> double),但缩小输入(即丢失

回答 1 投票 0

使用模板来防止缩小范围但允许扩大输入范围

我有一个类模板,旨在在浮点上进行模板化。 该类有一个方法,应该允许在需要时扩大输入(例如 float -> double),但缩小输入(即丢失

回答 1 投票 0

无法加载 Grapesjs 中保存的模板

无法加载 Grapesjs 中保存的模板

回答 3 投票 0

检查可调用签名参数是否是任何指针类型或可转换为特定类型?

我正在使用宏来进行编译时签名匹配,如下所示: #define ifSigMatch(sig) if constexpr (std::is_same_v(f)}), std::function 我正在使用宏来进行编译时签名匹配,如下所示: #define ifSigMatch(sig) if constexpr (std::is_same_v<decltype(std::function{std::forward<Foo>(f)}), std::function<sig>>) template <class T, class Foo> void afoo(Foo f) { ifSigMatch(void(T)) { ... } ifSigMatch(bool(T)) { ... } ifSigMatch(bool(T, int)) { ... } ... } 有没有办法处理Foo的第一个输入参数是任何指针类型的情况,或者参数可以隐式转换为T,而不需要向afoo添加更多模板参数? 最好是 c++17 或更低版本。 要处理 Foo 的第一个输入参数是任何指针类型或可隐式转换为 T 的情况,您可以将 SFINAE(替换失败不是错误)与 std::enable_if 结合使用。以下是您如何修改宏和模板函数afoo以实现此目的,而无需添加更多模板参数: #include <functional> #include <type_traits> #define ifSigMatch(sig) if constexpr (std::is_invocable_v<Foo, sig>) template <class T, class Foo> void afoo(Foo f) { ifSigMatch(T*) { ... } else ifSigMatch(std::enable_if_t<std::is_convertible_v<std::invoke_result_t<Foo, T>, T>, T>) { ... } else ifSigMatch(bool(T)) { ... } else ifSigMatch(bool(T, int)) { ... } // Add more conditions as needed // ... } 您可以使用具有类模板专门化的辅助类来获取返回类型和参数类型,然后在 constexpr 方法中使用它们来比较签名。 如果您想允许返回类型可转换为您指定的类型,可以使用 std::is_convertible_v 代替 std::is_same_v。 #include <type_traits> template <typename> struct Function; // This primary template is not defined. template <typename R, typename... Args> struct Function<R(Args...)> { template <typename Foo> constexpr bool matchesSig() { if constexpr (std::is_invocable_v<Foo, Args...>) { // only return true when the return type is EXACTLY as specified // otherwise, ifSigMatch(bool(T)) would also always imply // ifSigMatch(void(T)) to be true return std::is_same_v<std::invoke_result_t<Foo, Args...>, R>; } return false; }; }; #define ifSigMatch(sig) if constexpr (Function<sig>().template matchesSig<Foo>())

回答 2 投票 0

使用模板在一个函数中定义多个类的方法

假设我们有两个班级 A类{ 一个方法(); } B类{ B 方法(); } 方法函数做了一些重复的工作,例如,我们假设它只是返回类的副本。 我怎样才能

回答 3 投票 0

如何使用模板部分将 R quarto 中的作者左对齐到 pdf?

对于我的.qmd,我想更改部分pdf模板。我遇到了一些挑战。有人有想法吗? 左对齐标题和副标题:我通过更改部分 title.tex 来实现这一点 左对齐作者:

回答 1 投票 0

使用带有 helm 模板的 yaml 数组

我有一个表示 Helm 图表的目录结构,如下所示: Chart.yaml 值.yaml 模板/ 模板.tpl 值.yaml: foo:[“酒吧”,“巴兹”] FO: - 酒吧 - 巴兹 圣殿...

回答 1 投票 0

如何更改 phpstorm 自定义构造函数模板?

我使用 phpstorm 代码 > 生成... > 构造函数来获取: 函数 __construct() { } 但我想要: 公共函数 __construct() { } 如何更改模板?找不到任何东西并且...

回答 2 投票 0

如何在java中模板化JSON

我的用例是我有一个 json 文件,但我只需将其中的几个共享给客户端。 例如:考虑源 json 文件,如下所示。 { “名称”:“XYZ”, “年龄”:24, “教育”: { ”

回答 6 投票 0

使用变量参数进行模板专业化

我正在尝试将 c 风格的变体包装在库中。它使用类型枚举,我的意思是使用模板来填充。 简化版本大致如下所示: 枚举类型 { 类型_Int, 类型_Doub...

回答 4 投票 0

从可变替代方案中推导出 const 模板参数

当函数采用 const T 的 std::span 时,它无法从传入的可变跨度中推导出跨度类型。 然而,用户定义的类型是否可以允许扣除

回答 1 投票 0

使用变量参数的 C++ 模板专业化

我正在尝试将 c 风格变体包装在库中。它使用类型枚举,我的意思是使用模板来填充。 简化版本大致如下所示: 枚举类型{ 类型_Int, 类型_斗...

回答 1 投票 0

无法使用 cl.exe 编译任何模板代码

我正在尝试在 Windows 10 上使用 cl.exe 将一些 C++ 代码编译成 DLL 文件。由于错误 C2187,我根本无法使用模板编译任何代码,使用 Visual S...

回答 1 投票 0

将枚举数 T 静态转换为 std::underlying_type<T> 实际上会做什么?

我正在学习现代 C++,目前正在阅读有关模板和自动类型推导的内容。 作为挑战,我尝试编写书中的函数来解决问题,但结果有所不同......

回答 1 投票 0

通过href(html)发送数据并通过请求函数(django)获取

我有一个 html 页面,我在模型字段上循环。 html 是: {% for issues_list %} {{ Problem.id }} - {{ Problem.incident_date }} - {{ Problem.incident_specs }} - {{ 我有一个 html 页面,我在模型字段上循环。 HTML 是: {% for problem in problems_list %} <p> {{ problem.id }} - {{ problem.incident_date }} - {{ problem.incident_specs }} - {{ problem.incident_location }} - {{ problem.incident_reason }} {% for action in actions_list %} {% if action.action_incident_id_id == problem.id %} <form method="post"> <input hidden name='problem_id' value="{{ problem.id }} "> <a class="btn btn-primary" type="submit" href="{% url 'ex_action' %}"> Button </a> <!-- action="{% url 'ex_action' %}"--> </form> {% endif %} {% endfor %} </p> {% endfor %} 这是一个 ex_action 函数: def ex_action(request): return render(request, 'problems/ex_action.html', {'actions_list': actions_list}) 它工作得很好,但我需要将一个 Problem.id 从 for 循环传递到 ex_action 中,我不知道该怎么做。 我尝试了一些像href="{% url 'ex_action'?=problem_id=problem.id %}"这样的结构,并通过像request.GET.get('problem_id')或request.GET['problem_id']这样的东西来获取它,但是在html上得到了TemplateSyntaxError,在django代码或MultiValueDictKeyError上没有得到结果。 阅读了很多docs\issues,发现人们使用ajax\js来解决它,但我不知道如何解决。 请帮忙。 感谢 Zatigem 上面的回答(抱歉,花了很长时间 - 我试图应用你的答案,但忘记了),我们应该使用这样的结构: path('ex-action/int:problem_id',views.ex_action, name='ex_action'),将 Problem_id 传递给表单。 再次非常感谢,Zatigem!

回答 1 投票 0

模板参数的 HomoValueList 不起作用

问题是我在 Nicolai M. Josuttis 所著的《C++17 Complete Guide》一书中遇到了一段代码,第 122 页。该代码片段对我不起作用。我想知道为什么。 模板 问题是我在 Nicolai M. Josuttis 的一本名为 C++17 Complete Guide 的书中遇到了一段代码,第 122 页。该代码片段对我不起作用。我想知道为什么。 template<auto V1, decltype(V1)... VS> class HomoValueList { }; 作者声称这样的声明应该禁止非同构模板参数。但实际上我不能产生错误: int main() { HomoValueList<1, 'a', true> vals4; // Must be an error according to the author but not for me. return 0; } 这段代码是因为隐式转换而编译的,所以这本书是错误的。 我想知道自 C++17 标准发布以来是否发生了一些变化,因为作者声称必须禁止该行。 我不这么认为。即使在 C++17 模式下,最新的 Clang、GCC 和 MSVC 都接受此代码。 这是一个可行的 C++20 替代方案:(感谢 @PatrickRoberts) template <auto V1, auto ...VS> requires (std::is_same_v<decltype(V1), decltype(VS)> && ...) class HomoValueList {}; C++20 之前的版本,您可以使用 static_assert,但请注意,它不适合 SFINAE。 template<auto V1, auto ...VS> class HomoValueList { static_assert((std::is_same_v<decltype(V1), decltype(VS)> && ...)); };

回答 1 投票 0

C++ 类模板:定义超类参数化和 value_type 定义中使用的类型

我正在围绕迭代器编写一个小包装器,我的代码最终看起来像这样: 模板 类 类:公共超类 我正在围绕迭代器编写一个小包装器,我的代码最终看起来像这样: template <typename Iterator> class Class: public Superclass<typename std::remove_reference<Iterator>::type::value_type> { public: using value_type = typename std::remove_reference<Iterator>::type::value_type; explicit Class(Iterator&& iterator); protected: Iterator iterator; }; 类可以使用右值的 const 引用来构造,因此我在调用 std::remove_reference 后访问元素的类型,并使用此类型来参数化超类并定义 value_type。 有没有办法避免写 typename std::remove_reference<Iterator>::type::value_type 两次,例如通过定义本地类型? 谢谢, 查理 template<class Iterator, class ValueType=typename std::remove_reference<Iterator>::type::value_type> class Class: public Superclass<ValueType> { public: 等等

回答 1 投票 0

在“Software\NuGet\Repository”下找不到名为“AspNetMvc4VS11”的注册表项

当我尝试在 VS 2013 中使用 Durandal 模板创建新项目时,出现以下异常。 新建-->项目-->Web--> ASP.NET MVC 4 Web 应用程序--> Durandal SPA 项目...

回答 2 投票 0

在简单的 CRTP 案例中没有名为“XXX”的成员

这里我有一个简单的CRTP案例: #包括 #包括 模板 班级基础 { constexpr 静态 size_t impl_num = Impl::num; }; 模板 这里我有一个简单的CRTP案例: #include <cstddef> #include <utility> template <typename Impl> class base { constexpr static size_t impl_num = Impl::num; }; template <typename Impl> class deriv : public base<deriv<Impl>> { friend class base<deriv<Impl>>; constexpr static size_t num = Impl::num_in; }; class actual_impl { public: constexpr static size_t num_in = 10; }; using my_type = deriv<actual_impl>; int main() { my_type a{}; } 这个片段编译得很好,但是当我将基类更改为: #include <cstddef> #include <utility> template <typename Impl> class base { constexpr static std::make_index_sequence<Impl::num> idx{}; }; template <typename Impl> class deriv : public base<deriv<Impl>> { friend class base<deriv<Impl>>; constexpr static size_t num = Impl::num_in; }; class actual_impl { public: constexpr static size_t num_in = 10; }; using my_type = deriv<actual_impl>; int main() { my_type a{}; } Clang 抱怨error: no member named 'num' in 'deriv<actual_impl>'。我只是很困惑为什么第一种情况有效但第二种情况不起作用,这两种情况之间的根本区别是什么,因为在我看来,在这两种情况下 Impl::num_in 都在基类中使用。 一般来说,基类是否可以使用 Impl 中的 typedef 或 constexpr? 根本的区别是当您尝试访问 Impl 类的内部时。 Impl中的base<Impl>是一个不完整的类型,并且你可以用它做什么有一定的限制。 特别是,您无法访问 num 内的 base 数据成员,这就是该行的原因 constexpr static std::make_index_sequence<Impl::num> idx{}; 导致编译错误。请注意,要定义 base 类,编译器必须知道当时 Impl::num 的值。 与此相反,在第一个示例中,Impl::num仅用于初始化impl_num的值,否则不依赖于Impl::num。该初始化的实例化稍后发生,当 Impl 成为完整类型时。因此,没有错误。 如果稍微改变一下定义, template<typename Impl> class base { constexpr static decltype(Impl::num) impl_num = Impl::num; // or constexpr static auto impl_num = Impl::num; } 并使 impl_num type 依赖于 Impl,你会因为同样的原因得到同样的错误。 添加间接没有帮助,以下代码也无法编译: template<typename Impl> class base { constexpr static size_t impl_num = Impl::num; constexpr static std::make_index_sequence<impl_num> idx{}; }; 一般来说,基类是否可以使用 Impl 中的 typedef 或 constexpr? 这要看情况。您只能在 Impl 是完整类型时发生实例化的上下文中使用它们。例如, template<typename Impl> class base { public: void foo() { decltype(Impl::num) impl_num = 0; } }; 很好,但是 template<typename Impl> class base { public: decltype(Impl::num) foo() { return 0; } }; 不是。 避免 CRTP 中不完整类型潜在问题的标准技巧是引入辅助特征类: // Just forward declarations template<typename Impl> class deriv; class actual_impl; using my_type = deriv<actual_impl>; template<class> struct traits; template<> struct traits<my_type> { using num_type = std::size_t; }; template <typename Impl> class base { public: typename traits<Impl>::num_type foo() { return 0; } }; // Now actual definitions // ... 在这里,要访问 traits<Impl> 内部结构,Impl 不必是完整类型。

回答 1 投票 0

如何限制我的模板仅接受具有特定输入和输出类型的 lambda?

受到计算函数泰勒级数的其他问题的启发(原始问题),我编写了一个没有任何约束的模板来成功计算总和。这是当前代码(模板 b...

回答 2 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.