微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 警告级别为3的int的std :: vector push_back的编译器警告

我使用的是intel c编译器icc版本18.0.3.

如果我用-w3编译following code

#include <vector>

int main() {
    std::vector<int> vec;
    vec.push_back(2);
    return 0;
}

test_w3.cpp(6): remark #383: value copied to temporary,reference to
temporary used
vec.push_back(2);

Replacing一个const变量作为2

#include <vector>
int main() {
    std::vector<int> vec;
    const int a = 2;
    vec.push_back(a);
    return 0;
}

没有发出警告.

这个警告意味着什么?可以安全地忽略它(尽管需要无警告代码)吗?

解决方法

英特尔有一个网站正是针对这个问题确切地解决了您的问题 here.它是从2008年开始的,但似乎适用于您的问题.警告存在,因为此编程样式可能会导致隐藏的临时对象,并且在某些情况下可以忽略.

他们说明了这个例子:

void foo(const int &var)
{
}

void foobar()
{
    std::vector<std::string> numlist
        // 383 remark here: a tempory object with "123" is created
        numlist.push_back("123");       
    foo(10);       // gives 383

}

下列:

Resolution:

  • Provide a proper object for initializing the reference.
  • Can safely ignore this warning for pushback function of vector. The vector copies the argument into its own storage; it never stores the original argument. Therefore,using a temporary is perfectly safe.

所以你可能会忽略这个警告,即使这与一般规则相矛盾也绝不会忽视警告.

在我看来,英特尔选择了一种糟糕的方式,因为误诊导致的警告阻碍了发展.

原文地址:https://www.jb51.cc/c/118744.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐