8

Rather embarrassingly, I wrote something like the following (sanitised):

vector_item next()
{
    if (this->it == this->myVector.end()){
        this->it == this->myVector.begin();
    }
    return *(this->it++);
}

The obvious error is, well, obvious. It did however take a short while to track down.

No compiler warnings were generated for this, but should one have been? There is an unused r-value created right here (and not say, an unused return value from a function that was called for its side effects), which seems to me to be an indicator of there being something amiss with the code.

Compiled with g++ -Wall -Wextra. (GCC 4.8.3)

I know about -Wunused-result but that doesn't apply here.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Baldrickk
  • 4,291
  • 1
  • 15
  • 27
  • compiler version? – Richard Hodges Aug 23 '17 at 08:36
  • 1
    Unused return value are usual: you don't write `static_cast(std::cout << "hi");`. – Jarod42 Aug 23 '17 at 08:40
  • 2
    if `vector::iterator` is a class, then `operator ==` is not the built-in and is a regular function which might have side effect. – Jarod42 Aug 23 '17 at 08:42
  • @Jarod42: indeed. For example using VS2013, it only complains if they are built in types (C4553). Both assignment operator as begin() can have side effects. – gast128 Aug 23 '17 at 09:26
  • 1
    @Jarod42 hmm, according to the answers, clang takes it as a standard equality comparison. – Baldrickk Aug 23 '17 at 09:27
  • I thought that `-Wunused-value` might help (it does catch statements such as `3 == 4;`) but was surprised to find it doesn't. BTW, that sample is woefully incomplete; try `#include int main() { std::vector v; auto it = v.begin(); if (it == v.end()) { it == v.begin(); } return it == v.end(); }` instead. – Toby Speight Aug 23 '17 at 16:45
  • @TobySpeight I didn't intend for it to be runnable, just an illustration of usage. – Baldrickk Aug 24 '17 at 07:15

2 Answers2

6

No compiler warnings were generated for this, but should one have been?

I can't think of anything I've read in the standard that demands a warning here.

However, the clang dev team seem to think it warrants one:

18 : <source>:18:18: warning: equality comparison result unused [-Wunused-comparison]
        this->it == this->myVector.begin();
        ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
18 : <source>:18:18: note: use '=' to turn this equality comparison into an assignment
        this->it == this->myVector.begin();
                 ^~
                 =
1 warning generated.

My first thoughts are that it's a QoI issue in gcc. Might be worth raising it as an issue.

I am fortunate in that our software is compiled for mac (clang), linux (gcc) and windows (msvc) so standards infringements and edge cases are caught early.

Might be an idea to recompile your code on another compiler before going on a bug hunt now and again - it helps me.

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • Ah great, because when I wrote my answer I was afraid of missing something in the standard, happy to see that we agree. – gsamaras Aug 23 '17 at 08:50
  • 1
    _"Might be worth raising it as an issue"_ Having looked (now I know the clang flag, I've found [these](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62182) two [bugs](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53598) so it seems that it is in the system. – Baldrickk Aug 23 '17 at 09:22
4

No compiler warnings were generated for this, but should one have been?

There is nothing declared in the Standard that should make GCC generate a warning for that case.

You could extend begin(), by marking it as WARN_UNUSED, where first you would have define:

#define WARN_UNUSED __attribute__((warn_unused_result))

as described here, but this of course it's not exactly you are looking for, but it's something. I can't find any option of GCC to generate a warning in your case.

It is a known GCC bug though, but hasn't implemented the functionality you are looking for, at least until 2017-07-21.


However, clang 6.0.0 issues a warning for this, (even if Wall and Wextra flags are not used):

prog.cc:11:18: warning: equality comparison result unused [-Wunused-comparison]
        this->it == this->myVector.begin();
        ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
prog.cc:11:18: note: use '=' to turn this equality comparison into an assignment
        this->it == this->myVector.begin();
                 ^~
                 =
1 warning generated.

Moreover, zapcc 1.0.1 issues a warning as well (again even without the warning flags):

/home/jail/prog.cc:11:18: warning: equality comparison result unused [-Wunused-comparison]
        this->it == this->myVector.begin();
        ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/home/jail/prog.cc:11:18: note: use '=' to turn this equality comparison into an assignment
        this->it == this->myVector.begin();
                 ^~
                 =
1 warning generated.

View it yourself in Wandbox, if you like.

gsamaras
  • 71,951
  • 46
  • 188
  • 305