问题标签 [c++14]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
6 回答
5179 浏览

c++ - Raw pointer lookup for sets of unique_ptrs

I often find myself wanting to write code like this:

However, much of the std::set interface is kind of useless with std::unique_ptrs since the lookup functions require std::unique_ptr parameters (which I obviously don't have because they're owned by the set itself).

I can think of two main solutions to this.

  1. Create a temporary unique_ptr for lookup. For example, the above removeObject() could be implemented like:

    /li>
  2. Replace the set with a map of raw pointers to unique_ptrs.

    /li>

However, both seem slightly stupid to me. In solution 1, erase() isn't noexcept, so the temporary unique_ptr might delete the object it doesn't really own, and 2 requires double the storage for the container unnecessarily.

I know about Boost's pointer containers, but their current features are limited compared to modern C++11 standard library containers.

I was recently reading about C++14 and came across "Adding heterogeneous comparison lookup to associative containers". But form my understanding of it, the lookup types must be comparable to the key types, but raw pointers aren't comparable to unique_ptrs.

Anyone know of a more elegant solution or an upcoming addition to C++ that solves this problem?

0 投票
2 回答
924 浏览

c++ - 带有 constexpr 的编译时命名参数惯用语

我最近遇到了很多命名参数习语很有用的情况,但我希望它在编译时得到保证。在链中返回引用的标准方法几乎总是调用运行时构造函数(使用 Clang 3.3 -O3 编译)。

我无法找到任何与此相关的内容,因此我尝试使用它constexpr并获得一些功能:

虽然这对于少量参数是可以的,但对于大量参数,它很快就会变成一团糟:

有没有更好的办法?我正在考虑使用具有许多(30 多个)参数的类来执行此操作,如果将来扩展,这似乎很容易出错。

编辑: 删除了 C++1y 标签——而 C++1y 似乎确实解决了问题(感谢 TemplateRex!)这是用于生产代码的,我们被 C++11 困住了。如果这意味着它不可能,那么我想这就是它的方式。

EDIT2:为了说明我为什么要寻找这个,这里有一个用例。目前使用我们的平台,开发人员需要为硬件配置显式设置位向量,虽然这没关系,但很容易出错。有些使用来自 C99 扩展的指定初始化程序,这没问题,但不标准:

然而,大多数人甚至没有使用它,而只是输入了一大堆数字。因此,作为一项工作改进,我想朝着这样的方向发展(因为它也强制使用更好的代码):

这可能更冗长,但稍后阅读时会更清晰。

0 投票
5 回答
1235 浏览

c++ - 专门化 std::optional

是否可以专门std::optional针对用户定义的类型?如果不是,那么提出这个标准是否为时已晚?

我的用例是一个类似整数的类,它表示一个范围内的值。例如,您可以有一个位于 [0, 10] 范围内的整数。我的许多应用程序甚至对单个字节的开销都很敏感,因此std::optional由于额外的bool. 但是,对于std::optional范围小于其基础类型的整数,特化 for 将是微不足道的。我们可以简单地将值存储11在我的示例中。这应该不会为非可选值提供空间或时间开销。

我可以在 中创建这个专业namespace std吗?

0 投票
1 回答
339 浏览

c++ - 测试对齐存储的大小

为了测试特定类型是否适合aligned_storage,我创建了以下测试结构:

现在我有点想知道这样的测试是否/将出现在标准库中。不愿重新发明轮子。

我正在使用它来检查定义aligned_storage的(大小Bytes)标头是否可以采用内部数据类型,该数据类型仅在实际编译单元中可用。

0 投票
5 回答
8137 浏览

c++ - C++1y/C++14:变量模板特化?

根据 C++1y/C++14 N3690,变量模板特化的类型是否必须与主模板的类型相同?

如果是这样,是否有可能以某种方式使主要未定义?

草案中的哪些内容?

类模板的等效功能是:

0 投票
2 回答
772 浏览

c++ - 什么是脱离的 std::optional 的哈希值目的?

cppreference 中的此页面提到std::hash已专门用于std::optional,但未指定对象脱离时的行为。我可以想到不同的行为:

  • 它可以抛出一个std::bad_optional_access, 以保持一致std::optional::value
  • 它可以为每个 disengaged 返回相同的哈希std::optional<T>,这样 2 disengaged 对象将具有相同的哈希。
  • 它可以返回一个std::optional<std::hash<std::optional<T>>>
0 投票
2 回答
997 浏览

c++ - 是否会在 c++1y 标准中引入 biginteger 等效项

我记得不久前我听说有人想在 c++0x 标准中包含一个 BigInteger 实现(因为它当时被称为)。显然这没有发生,但我想知道这是否计划作为 c++1y 的一部分。

0 投票
5 回答
3166 浏览

c++ - 在 C++11 或 C++1y 中对非类型模板参数包进行排序?

在 C++11 和/或 C++1y 中:

假设我得到一个带有非类型参数包的模板:

我正在编写另一个模板来实例化它:

我希望 g 按排序顺序用 x 实例化 f 。

那是:

应该调用:

这可以做到吗?如果是这样,最有效的方法是什么(直到恒定因素)?

0 投票
2 回答
235 浏览

c++ - C++14 结合通用 lambda 和变量模板

我知道通用 lambdas,也知道变量模板,但是,这是做什么的?甚至允许吗?

如果允许,是否可以按预期使用?也就是说,作为f<type>(var_a, var_b)?

0 投票
1 回答
372 浏览

c++ - C++ 中 [[pure]] 的说明

阅读了关于标准 C++ 属性[[pure]]的最新提案,我遇到了一些问题:

  1. [[pure]] 函数可以读取 const 全局变量吗?
  2. [[pure]] 函数可以读取静态 const 变量吗?
  3. [[pure]] 函数是否可以写入但不读取指针或引用变量(并且仍然可能返回 void)?