5

I am using the intel c++ compiler icc version 18.0.3.

If I compile the following code with -w3

#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 the 2 with a const variable as

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

does not give a warning.

What does this warning mean? Can it safely be ignored (although warning free code is desirable)?

bolov
  • 72,283
  • 15
  • 145
  • 224
schorsch312
  • 5,553
  • 5
  • 28
  • 57
  • Does it give same remark if vector is declared outside of main function or if you print result? – huseyin tugrul buyukisik Sep 11 '18 at 10:00
  • If I print it the remark is still given. https://gcc.godbolt.org/z/atlYe6 – schorsch312 Sep 11 '18 at 10:07
  • accoridng to this post [intel-c-compiler/topic/300453](https://software.intel.com/en-us/forums/intel-c-compiler/topic/300453). The remark is unimportant and by the sounds of it, should of been removed, it might be a bug. – George Sep 11 '18 at 10:07
  • If I move to a function the remark is also still given. https://gcc.godbolt.org/z/fKoED3 – schorsch312 Sep 11 '18 at 10:09
  • It really looks like a mistaken warning. Even icc doesn't complain about the same code if you replace `-w3` with `-Wall` – Useless Sep 11 '18 at 10:14

1 Answers1

10

Intel has a site for exactly this issue with exactly your problem here. It is from 2008, but seems to be applicable to your problem. The warning exists, as this programming style may lead to hidden temporary objects and can be ignored in some cases.

They state for this example:

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

}

The following:

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.

So you may ignore the warning, even though this contradicts the general rule never ignore warnings.

In my opinion Intel has chosen a bad way, as warnings which result from misdiagnosis hinder development.

Community
  • 1
  • 1
  • 9
    Ignoring warnings is unfortunately a pretty bad idea since this means that *real* warnings will drown in the false-positive noise. That’s why such warnings (which are likely to be false-positives) are a bad idea to begin with. This is a bad warning. It should be disabled globally in the command-line options. – Konrad Rudolph Sep 11 '18 at 10:57
  • 4
    I completely agree –  Sep 11 '18 at 11:11
  • @KonradRudolph Added a warning about ignoring warnings! Thanks for you suggestion! –  Sep 11 '18 at 13:05
  • Shouldn't one use `emplace_back()` here anyway, in reality? ICC should not warn for that (I don't know if it *does*, but it *shouldn't*). My PoV is only to use `push` methods to copy/move an existing instance (i.e. not a temporary), but if creating a new element from ctor args, they should be forwarded by `emplace` rather than using a temporary as a vehicle. To me, that both states intent more clearly and can avoid wasteful copies/moves - and, I learned today, warnings! – underscore_d Sep 11 '18 at 15:53
  • @underscore_d neither push_back nor emplace_back should emit a warning, but they do (https://gcc.godbolt.org/z/1BDtLX for an emplace_back example). –  Sep 11 '18 at 16:22
  • @underscore_d Sorry, missunderstood your comment. Yes emplace_back would create the integer in place. But for the sample provided this is not going to make any difference ;) –  Sep 11 '18 at 20:39