templates 相关问题

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

如何在成员函数作用域内定义成员函数?

这个问题是关于易用性的。我的最终愿望是,当我折叠除类主体范围之外的类代码时,我所看到和读到的内容尽可能简短和必要,这意味着所有

回答 1 投票 0

如何检测模板函数应该施加更严格的概念

我有一些代码,如下所示: #包括 #包括 #包括 模板 自动减少(Iter i1,Iter i2) { 返回我...

回答 1 投票 0

调用时模板参数推导/替换失败

要么是我对 C++ 的理解有问题,要么是某个地方存在隐藏的拼写错误......我在这几个 LOC 上浪费了几个小时。 首先,我定义了一个 start() 方法: 模板 要么是我对 C++ 的理解有问题,要么是某个地方存在隐藏的拼写错误...我在这几个 LOC 上浪费了几个小时。 首先,我定义了一个 start() 方法: template <typename FunctionResult, typename ...FunctionArgs> inline std::shared_ptr<StoppableThread> start( std::function<FunctionResult(const bool&, FunctionArgs...)> function, FunctionArgs... args) { ... } 还有一些 threadedSearch() lambda 函数: std::function<void(const bool&, const Source&, Population&, const std::vector<Parameter>&, const size_t&, const size_t&)> threadedSearch = ... ; 但是当我尝试执行此操作时: Source source = { ... }; Population population = { ... }; const std::vector<Parameter> variationsParameters = { ... }; const size_t randomCandidatesCount = ..., lineageCount = ...; auto searchThread = start( threadedSearch, source, population, variationsParameters, randomCandidatesCount, lineageCount); 编译器不同意后者对 starŧ() 的调用,并告诉我: # with g++ error: no matching function for call to ‘start(std::function<void(const bool&, const Source&, Population&, const std::vector<Parameter>&, const long unsigned int&, const long unsigned int&)>&, Source&, Population&, const std::vector<Parameter>&, const size_t&, const size_t&)’ [...] note: candidate: ‘template<class FunctionResult, class ... FunctionArgs> std::shared_ptr<StoppableThread> start(std::function<FunctionResult(const bool&, FunctionArgs ...)>, FunctionArgs ...) note: template argument deduction/substitution failed: note: inconsistent parameter pack deduction with ‘const Source&’ and ‘Source’ # with clang++ error: no matching member function for call to 'start' note: candidate template ignored: deduced conflicting types for parameter 'FunctionArgs' (<const Source&, Population&, const std::vector<Parameter>&, const unsigned long&, const unsigned long&> vs. <Source, Population, std::vector<Parameter>, size_t, size_t>) 我的问题是:WTF? 还有:我能做什么?在 start<...>() 调用中显式指定模板参数甚至不起作用... 我不知道如何让编译器理解它应该看到的“真实”参数类型...... 可以在这里找到完全崩溃的最小示例:https://onlinegdb.com/FtBIGmkH- 编辑 好的,用它来编译: auto searchThread = start<void, const Source&, Population&, const std::vector<Parameter>&, const size_t&, const size_t&>( threadedSearch, source, population, parameters, randomCandidatesCount, lineageCount); 谢谢你们,隔离一个最小的示例确实帮助我进行了调试! whatever_t start(std::function<FunctionResult(const bool&, FunctionArgs...)> function, FunctionArgs... args) 这出现了两次FunctionArgs...。当属于 std::function 类型的一部分时,上下文是不可推导的,并且包将按原样从 std::function 中获取。在另一种情况下,上下文是可推导的,因此包将从实际的函数参数中推导出来。 在两种情况下,两个包都应该完全相同。然而 FunctionArgs... args 永远不会推导出引用类型,并且你的函数接受 const 引用。因此,一个包将充满 const 引用类型,而另一个包将充满非引用类型。这是替换失败。 解决该问题的一种方法是使其他事件也不可推论。 whatever_t start(std::function<FunctionResult(const bool&, FunctionArgs...)> function, std::type_identity_t<FunctionArgs>... args) 将完成这项工作(需要 C++20,但可以在紧要关头实现相当于 std::type_identity_t 的功能)。 另一种方法是将函数类型与参数解耦。 template<typename Func, typename ... Args> whatever_t start(Func&& func, Args&& ... args) (为了确保可以使用 func 调用 args,您可以使用 requires 子句或老式的 SFINAE,或者直接调用它,并在无法调用时处理不太有用的错误消息),

回答 1 投票 0

C++:在抽象类中定义 begin() 和 end()

我在开发优化求解器 Uno 时遇到了问题。 我定义了一个抽象类 Collection ,它表示一组我可以迭代的整数:一个向量、一个数值范围(一组

回答 1 投票 0

为什么动态调度在模板函数中不起作用?

我有以下代码。有一个基类 (Node) 和一个派生类 (NumberExprNode),每个类都有一个虚拟相等方法。 #包括 #包括 #包括<

回答 1 投票 0

参数数量在编译时确定的 Lambda 函数

我想声明一个具有 N 个参数的 lambda 函数,其中 N 是模板参数。就像是... 模板 A类{ 标准::函数 我想声明一个具有 N 个参数的 lambda 函数,其中 N 是模板参数。类似... template <int N> class A { std::function<void (double, ..., double)> func; // exactly n inputs }; 我想不出用元函数范例来做到这一点的方法。 您可以使用嵌套的 typedef n_ary_function 编写模板 type。该类型可以如下使用: template <int N> class A { typename n_ary_function<N, double>::type func; }; 以下代码片段包含n_ary_function的定义: template <std::size_t N, typename Type, typename ...Types> struct n_ary_function { using type = typename n_ary_function<N - 1, Type, Type, Types...>::type; }; template <typename Type, typename ...Types> struct n_ary_function<0, Type, Types...> { using type = std::function<void(Types...)>; }; 元template采用模板、计数和类型,并使用类型的N副本调用模板: template<template<class...>class target, unsigned N, class T, class... Ts> struct repeat_type_N: repeat_type_N<target, N-1, T, T, Ts...> {}; template<template<class...>class target, class T, class... Ts> struct repeat_type_N<target, 0, T, Ts...> { typedef target<Ts...> type; }; template<template<class...>class target, unsigned N, class T> using repeat_type_N_times = typename repeat_type_N<target, N, T>::type; 现在,我们使用它: template<typename... Ts> using operation=void(Ts...); template<unsigned N, class T> using N_ary_op = repeat_type_N_times< operation, N, T >; template<unsigned N> using N_double_func = N_ary_op<N,double>; 我们测试一下: void three_doubles(double, double, double) {} int main() { N_double_func<3>* ptr = three_doubles; std::function< N_double_func<3> > f = three_doubles; } 并获胜。 您到底使用double, double, double做什么完全取决于您在上述系统中的情况。例如,您可以使用 lambda 来初始化 std::function。 您可以将 double, double, double 打包到 template<class...>struct type_list{}; 中,这样您就可以将其作为一个参数传递给另一个 template,然后专门对其进行解包。 A repeat_type 对于大 N 具有较少的递归: // package for types. The typedef saves characters later, and is a common pattern in my packages: template<class...>struct types{typedef types type;}; // Takes a target and a `types`, and applies it. Note that the base has no implementation // which leads to errors if you pass a non-`types<>` as the second argument: template<template<class...>class target, class types> struct apply_types; template<template<class...>class target, class... Ts> struct apply_types<target, types<Ts...>>{ typedef target<Ts...> type; }; // alias boilerplate: template<template<class...>class target, class types> using apply_types_t=typename apply_types<target,types>::type; // divide and conquer, recursively: template<unsigned N, class T, class Types=types<>> struct make_types:make_types< (N+1)/2, T, typename make_types<N/2, T, Types>::type > {}; // terminate recursion at 0 and 1: template<class T, class... Types> struct make_types<1, T, types<Types...>>:types<T,Types...> {}; template<class T, class Types> struct make_types<0, T, Types>:Types{}; // alias boilerplate: template<unsigned N, class T> using make_types_t=typename make_types<N,T>::type; // all of the above reduces `repeat_type_N_t` to a one-liner: template<template<class...>class target, unsigned N, class T> using repeat_type_N_times = apply_types_t<target, make_types_t<N,T>>; 对于大型N,上述可以显着减少编译时间,并处理template堆栈溢出。 您不能直接执行此操作。 你可以做这样的事情 template <unsigned N> class UniformTuple; template <> class UniformTuple <0> { }; template <unsigned N> class UniformTuple : public UniformTuple <N-1> { public: template <typename... Args> UniformTuple (double arg, Args... args) : UniformTuple <N-1> (args...) , m_value (arg) { } private: double m_value; }; template <int N> class A { std :: function <void (const UniformTuple <N> &)> func; }; 为了完整起见,这是一个不使用递归的解决方案: template <class Ret, class Arg, class Idx> struct n_ary_function_; template <class Ret, class Arg, std::size_t... Idx> struct n_ary_function_<Ret, Arg, std::index_sequence<Idx...>> { template <class T, std::size_t> using id = T; using type = std::function<Ret(id<Arg, Idx>...)>; }; template <class Ret, class Arg, std::size_t N> using n_ary_function = typename n_ary_function_< Ret, Arg, std::make_index_sequence<N> >::type; 在 Coliru 上观看直播 NoSid 的解决方案非常有创意(本质上是一一“追加”类型)。我编写的另一个解决方案可能需要更少的脑力劳动,如下所示使用 std::index_sequence (这是创建未知大小的参数包的一种非常自然的方法): #include <utility> #include <functional> #include <type_traits> template<typename T, long U> struct reduce { using type = T; }; template<typename U, typename IndexSequence> struct FunctionHolderImpl; template<typename U, long ... Indices> struct FunctionHolderImpl<U, std::index_sequence<Indices...>> { using value = std::function<void(typename reduce<U, Indices>::type...)>; }; template<long N> struct FunctionHolder { using func = FunctionHolderImpl<double, std::make_index_sequence<N>>::value; };

回答 5 投票 0

如何在 Node.js 中为我的视图之一排除或指定备用layout.mustache?

当前所有页面都是从views/layout.mustache 文件和特定于页面的views/page.mustache 模板呈现的 我想使用替代的layout.mustache和/或跳过layout.mustache全部...

回答 1 投票 0

如何在 Node.js 中为我的视图之一排除或指定备用layout.mustache?

当前所有页面都是从views/layout.mustache 文件和特定于页面的views/page.mustache 模板呈现的 我想使用替代的layout.mustache和/或跳过layout.mustache全部...

回答 1 投票 0

模板类型参数相对于引用和指针的函数重载解析

我一直在编写一些代码作为练习。 模板 const T& 较大(const T& a, const T& b) { std::cout << "Calling larger(const T&, const T&am...

回答 1 投票 0

我的主导航页面没有显示预览

在 Microsoft Dynamics 365 Power Pages 中,我尝试使用模板创建门户 创建后,我尝试编辑主导航上的页面 但是,当单击任何主导航时...

回答 1 投票 0

如何使用递归下降解析器解析C++的模板语法?

在出于学习目的编写 C++ 解析器时,我有一个问题。 整数a,b; 模板 T Foo() {} int main() { 布尔 x = a < b; auto y = Foo(); } 在这里,<

回答 1 投票 0

如何在类定义之外的模板类中定义模板成员函数?

鉴于: 模板 类 Foo { 民众: 模板 无效栏(); }; 如何在类定义之外实现 bar,同时仍然可以访问两个模板

回答 1 投票 0

如何将模板定义和实例化拆分为 hpp 和 ipp 文件[重复]

我想将我的模板类分成 2 个文件,就像普通类如何使用声明所在的 .hpp 和实现所在的 .ipp 发挥作用一样。 我已经和 Nor 一起工作了...

回答 1 投票 0

如何编写一个 C++ 概念来检查 numeric_limits 是否支持类型

我想编写一个 C++ 概念来检查 numeric_limits 库是否支持给定类型。我编写了以下代码来执行此操作: // 测试.cc #包括 模板<

回答 1 投票 0

如何为C++中的所有派生类专门化模板函数?

我有代码: 基类{}; 模板 void Print(const T& obj); 我想专门针对从 Base 派生的所有类进行打印。 我尝试了这个概念: 模板

回答 1 投票 0

在 WPF 中的不同控件上显示验证错误模板

我有一个包含其他控件和一个文本框的用户控件。它有一个绑定到 TextBox 文本的 Value 属性,并将 ValidatesOnDataErrors 设置为 True。 当发生验证错误时...

回答 1 投票 0

带有CodeIgniter的Smarty模板,当层次结构中的目录太多时无法加载模板

我正在使用 CI Smarty https://github.com/Vheissu/Ci-Smarty 据我所知,这有两个问题。但我打开这个问题的问题是我无法加载 .tpl 文件,如果......

回答 5 投票 0

std::ostream 运算符的 C++ 问题<< in template class

如果我将类代码放在同一个头文件或同一个 .cpp 文件中,我就不会遇到这个问题,但是当我将类规范放在头文件中并将类代码放在单独的 .cpp 文件中时,我得到了这个

回答 1 投票 0

如何从 dotnet 新模板执行 postaction 脚本

我试图在从 dotnet 模板生成项目后执行一个简单的脚本。该脚本将尝试在生成的项目的根目录中创建一个名为 new.txt 的新文件。 一些

回答 1 投票 0

如何处理和修复垃圾数据?

问题状态 我有一个名为 Group 的模板类,它可以访问内存中未经授权的位置。成员变量 Group::Items 指向存储

回答 1 投票 0

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