小编H. *_*ich的帖子

在地图中使用unique_ptr时删除std :: pair中的函数

我有一段C++代码,我不确定它是否正确.请考虑以下代码.

#include <memory>
#include <vector>
#include <map>

using namespace std;

int main(int argc, char* argv[])
{
    vector<map<int, unique_ptr<int>>> v;
    v.resize(5);

    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

GCC编译这段代码没有问题.但是,英特尔编译器(版本19)因错误而停止:

/usr/local/ [...] /include/c++/7.3.0/ext/new_allocator.h(136): error: function "std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2> &) [with _T1=const int, _T2=std::unique_ptr<int, std::default_delete<int>>]" (declared at line 292 of "/usr/local/ [...] /include/c++/7.3.0/bits/stl_pair.h") cannot be referenced -- it is a deleted function
    { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
                            ^
      detected during:

[...]

instantiation of "void std::vector<_Tp, _Alloc>::resize(std::vector<_Tp, _Alloc>::size_type={std::size_t={unsigned long}}) [with _Tp=std::map<int, std::unique_ptr<int, std::default_delete<int>>, std::less<int>, std::allocator<std::pair<const int, std::unique_ptr<int, …
Run Code Online (Sandbox Code Playgroud)

c++ icc c++14

10
推荐指数
1
解决办法
621
查看次数

std::bind 是否忽略了符合标准的多余参数?

返回的函子std::bind忽略多余的参数 [ SO1 , SO2 , SO3 ]。一个简单的例子如下所示。

#include <iostream>
#include <functional>

void foo(int a) {
  std::cout << "a=" << a << std::endl;
}

int main() {
  auto f = std::bind(foo, 42);
  f(3141); // a=42
}
Run Code Online (Sandbox Code Playgroud)

很明显,std::bind以这种方式实施更容易 [ SO1a ]。然而,这种对多余论点的忽略是标准化的还是未定义的行为?

c++ c++11

5
推荐指数
1
解决办法
65
查看次数

标签 统计

c++ ×2

c++11 ×1

c++14 ×1

icc ×1