c++14 相关问题

C ++ 14是2014年批准的C ++标准的名称。它基于以前的C ++ 11标准,改进了核心语言和标准库并添加了一些功能。

C++ STL 列表中派生类对象的多态行为

我将基类和派生类(Account、StudentAccount、EmployeeAccount)的对象存储在 STL 列表中,并使用 STL 迭代器处理它们。对象的动态多态行为并不...

回答 2 投票 0

在地图中添加指向地图中键的指针

静态 std::mapnid; pnode* ptrof(pnode &pn) { auto r = nid.insert( {pn, nullptr} ); if (r.second) { nid[pn] = &(r.first->first); ...

回答 1 投票 0

C++ 模板、链接器错误和设计模式

请注意:此代码可能看起来过于复杂,或者引发有关 Convert 函数如何执行的进一步问题,但我故意只展示一个非常小的示例和

回答 1 投票 0

为什么 constexpr 函数中的参数不是常量表达式?

您能解释一下为什么这段代码无法编译吗? // 源.cpp constexpr const char* func(const char* s) { return s;} constexpr bool find(const char *param) { constexpr const char* 结果...

回答 1 投票 0

如何推断 std::array 的大小?

在以下代码中: 模板 int b(int q, const std::array& 类型) { 整数 r = q; for (int t : 类型) { r = r + t; } 返回 r; } 整数...

回答 4 投票 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

可重用::testing::gtest中的值生成器

我有一个软件(c++),它通过 googletest 进行了过度测试。 有很多 INSTANTIATE_TEST_SUITE_P 调用。 有一个我想重用的特定值生成器,某些东西......

回答 1 投票 0

寻找一种在 C++ 中计算某些值的有效方法[已关闭]

我正在用C++编写代码。我有一些向量,现在我使用 for 循环来计算我需要的向量。但是,事实证明这是非常耗时的,特别是当向量的大小是相对的时......

回答 1 投票 0

有理由不使用最新的C++标准吗?

我发现IDE中的默认标准通常不是最新发布的标准,甚至不是IDE中最新的标准。 例如 JetBrains 的 Clion 有 C++20 和 C++17,但默认...

回答 1 投票 0

Codeforces 607A。得到错误的答案

有 n 个信标位于数轴上的不同位置。第 i 个信标的位置为 ai,功率级别为 bi。当第 i 个信标被激活时,它会摧毁其左侧的所有信标(直接...

回答 1 投票 0

c++14中__cplusplus有标准定义吗?

我正在寻找设置一些预处理器的东西,并且我想要一个更准确的数字来说明 C++14 中的 __cplusplus 应该定义为什么。标准中有规定吗?

回答 2 投票 0

C++ 的 if-else 梯形图必须在最后写 else 语句吗?

我总是看到,在 C++ 中的 if-else 梯形图的末尾,底部的 else 语句始终存在。 如果(条件1){ doA(); } else if (条件2) { doB(); } else if (条件3) { ...

回答 1 投票 0

C++标准核心语言规范中的注释和示例是否不规范?

我经常在 SO(和其他地方)上看到这样的说法:C++ 标准中的注释和示例不规范 - 我自己可能已经多次提出过这样的说法。 然而我找不到证据...

回答 3 投票 0

字符串构造函数将两个 char* 放入另一个 std::string 在 c++14 中有效,但在 c++17 中无效

以下程序尝试使用第一个字符串和指向第一个字符串中间的指针构造第二个字符串: #包括 int main() { std::string src = "你好

回答 2 投票 0

在c++中,内存将分配给全局函数,并且全局函数的地址会在运行时改变还是不会改变?

//绑定示例 #include // std::cout #include // std::bind // 一个函数: (也适用于函数对象: std::divides my_divide;) 双

回答 1 投票 0

使用 lower_bound 在矩阵的一行中查找序列

我需要编写一个函数,它将按指定的标准对矩阵(向量>)的行进行排序。标准是如果一行的最大元素......

回答 1 投票 0

我可以将 MISRA-2023 与 C++14 一起使用吗?

我想遵循 MISRA C++:2023 编码指南。但它的目标是 C++17,而代码和编译器则遵循 C++14。 文件内容如下: 本文档中的指南以 C++17 为目标,...

回答 1 投票 0

libmysqlclient.a(client.c.o) 与 elf_i386_fbsd 不兼容

我正在尝试使用 gcc++9.3 和 FreeBsd 12.1 编译我的程序,并在过程结束时给我这个错误 root@royal-server:/usr/src/sursa/Server/source/game/src # gmake -j20 正在链接...

回答 1 投票 0

保存压缩的dicom图像,而无需暂时保存未压缩的dicom

我有一个继承自 DcmDataset 类的 DicomInterface 类。设置 Dicom 标签后,我想使用 JPEG Process 14 传输语法保存 DICOM 图像。如果我首先保存 Dicom 文件...

回答 1 投票 0

如何分配字符串文字

我是一名Java程序员;我最后一次编写 C 或 C++ 是在 20 年前。现在我回来了,我正在尝试使用更现代的 C++,例如 C++11/C++14,并避免旧的 C 风格编程。 如何分配一个字符串,...

回答 2 投票 0

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