- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
鉴于这个程序:
struct Val
{
Val() = default;
Val(Val&&) = default;
auto& operator=(Val&&);
};
/* PLACEHOLDER */
auto& Val::operator=(Val&&) { return *this; }
替换 /* PLACEHOLDER */
与...
int main()
{
std::vector<std::pair<int, Val>> v;
v.emplace(std::begin(v), 0, Val{});
}
...编译成功:
g++ 7.0.1(主干)
clang++ 3.9.1
替换 /* PLACEHOLDER */
与...
template <typename TVec>
void a(TVec& v)
{
v.emplace(std::begin(v), 0, Val{});
}
int main()
{
std::vector<std::pair<int, Val>> v;
a(v);
}
...编译成功:
...但在以下位置产生编译时错误:
产生的错误似乎与受约束的 pair operator=(pair&&)
有关过载 - from include/bits/stl_pair.h
on GitHub's libstdc++ mirror :
pair&
operator=(typename conditional<
__and_<is_move_assignable<_T1>,
is_move_assignable<_T2>>::value,
pair&&, __nonesuch&&>::type __p)
noexcept(__and_<is_nothrow_move_assignable<_T1>,
is_nothrow_move_assignable<_T2>>::value)
{
first = std::forward<first_type>(__p.first);
second = std::forward<second_type>(__p.second);
return *this;
}
代入is_move_assignable<_T2>
与 std::true_type
允许代码编译。
move Val::operator=(Val&&)
的定义之前 /* PLACEHOLDER */
允许代码编译。
更改 auto& Val::operator=(Val&&)
至Val& Val::operator=(Val&&)
允许代码编译。
这是怎么回事?这是最新版本的 libstdc++ 中的实现缺陷吗?还是旧版本错误地编译了格式错误的代码?
编辑:如 AndyG在他的 (现已删除) 答案中发现,在调用 emplace
之前调用空函数时也会发生错误。 :
template <typename TVec>
void a(TVec&) { }
int main()
{
std::vector<std::pair<int, Val>> v;
a(v);
v.emplace(std::begin(v), 0, Val{});
}
退出a(v);
以上可防止产生编译时错误。 这种行为在 g++7 和 clang++5 中都存在。
Sergey Murzin 发现了另一个奇怪的案例,并且可以测试出on wandbox :
int main()
{
std::vector<std::pair<int, Val>> v;
v.emplace(v.begin(), 0, Val{});
std::cout << v.back().first << std::endl;
}
上面的代码会产生编译错误。注释掉包含 std::cout
的行防止错误发生。 这种行为在 g++7 和 clang++5 中都存在。
最佳答案
这几乎可以肯定是实例化点问题。如果你做了一些触发 pair<int, Val>
的实例化的事情的定义在 main
,然后你得到错误。否则,它只会在 vector::emplace
时被实例化。被实例化,这里讨论的实现推迟到翻译单元的末尾(这是允许的,请参阅 [temp.point]/8 ),此时赋值运算符已完全定义并可调用。
a(v)
触发 pair<int, Val>
定义的实例化因为 ADL 需要它。如果你写 ::a(v)
或 (a)(v)
(都抑制 ADL)然后错误消失。 (很明显,a.back().first
需要 pair<int, Val>
的实例化:您正在访问它的数据成员。)
关于c++ - `pair::operator=(pair&&)` 错误与 `auto&` 推断 move 操作 - libstdc++ 回归?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41956691/
当使用模板模板参数时,我如何推断或删除模板模板的模板类型? 考虑以下 SSCCE: #include #include #include using namespace std; templat
假设我有一些特质: trait A[T] { def foo: T } 一个扩展它的类: class B[T](t: T) extends A[T] { def foo = t } 以及父特征的子特征
一边玩-rectypes在某些时候选择 OCaml 我只是迷路了。 这个表达式几乎可以打字: # fun x -> x x;; - : ('a -> 'b as 'a) -> 'b = 但是这里 O
我正在编写一个类似 CRUD 的应用程序,并且通过主键进行大量查找(主键可以有不同的类型)。所以我定义了以下类型类: {-# LANGUAGE MultiParamTypeClasses #-} cl
我已经创建了关系 A 'is functional parent of' B并定义 'has functional parent'作为 'is functional parent of' 的倒数. '
给定一个使用 Kotlin 版本 1.3.61 和 JOOQ 版本 3.13.1 的系统,这样的方法会构建 union正常查询: val selectCommonPart = coalesce
考虑以下错误代码: fun x = if (null x) then 0 else (take 50 x) : (fun (drop 50 x)) 我注意到,我可以毫无问题地将它加载到
给定一个具有以下类型的函数 a: a::x -> Bool 和以下类型的另一个函数 b: b::Bool -> y 我正在尝试找出推断以下函数类型的步骤: c =\d -> d a b 有人可以帮助解
我正在尝试使用 Infer 工具来分析我的应用代码。我关注了these steps每次我尝试运行 infer -- gradle build 时,我都会收到以下错误: infer -- gradle
所以我制作了这个模板来定义内联仿函数: template struct AsFunctor { template std::invoke_result_t operator()(A
是否可以推断 CRTP 基类中模板化成员函数的返回类型? 虽然推断参数类型效果很好,但它因返回类型而失败。考虑以下示例。 #include template struct base { tem
使用 Series.interpolate 很容易在 Pandas.DataFrame 中插入值,如何进行外推? 例如,给定一个如图所示的 DataFrame,我们如何将它外推 14 个月到 2014
我想知道为什么这不起作用(缺少参数类型)? Seq(1,2,3).toSet.map(_ + 1) 但这确实: val foo = Seq(1,2,3).toSet foo.map(_ + 1)
我没有必要使用 SQLite3 shell 工具来维护一个小型数据库。我正在使用 -header -ascii标志,尽管据我所知,这适用于任何输出选择。我正在寻找一种方法来避免对返回的任何一个值的类型
我有以下组件 type PropTypes = { items: T[], header: (item: T) => React.Element, body: (item: T) => R
我想在 Eclipse/JSDT 中指定实例变量的类型,如下例所示: /** * @constructor */ function A() { /** @type Node */
我正在用 Python 编写一个方法,它看起来像这样: def rgb_to_grayscale(image): print(image.shape) pass 此处预期的类型是 nu
我有一个 my_values 数组,我正在尝试为其推断 true_values 数组中最接近、较小的值。使用下面的 find_nearest 函数并不能完成我想要的。我如何追加它以找到最近的、较小的值
在下面的代码中: template int b(int q, const std::array& types) { int r = q; for (int t : types)
在 Pandas DataFrame 中插入 NaN 单元非常容易: In [98]: df Out[98]: neg neu pos av
我是一名优秀的程序员,十分优秀!