Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do I get "must be modifiable l-value" compiler error (C2105) on following code?
    primarykey
    data
    text
    <p>I'm stumped by this one...the compiler outputs:</p> <p>d:\data\personal\projects\test\test.cpp(42): error C2105: '--' needs l-value</p> <pre><code>#include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;iterator&gt; class FOO { public: typedef int* iterator; typedef const int* const_iterator; class explicit_iterator : public std::iterator&lt; std::bidirectional_iterator_tag, int &gt; { public: explicit_iterator(int* ptr = nullptr) : m_ptr(ptr) {} bool operator ==(const explicit_iterator&amp; rhs) const { return m_ptr == rhs.m_ptr; } bool operator !=(const explicit_iterator&amp; rhs) const { return m_ptr != rhs.m_ptr; } reference operator *() const { return *m_ptr; } explicit_iterator&amp; operator ++() { ++m_ptr; return *this; } explicit_iterator&amp; operator --() { --m_ptr; return *this; } private: int* m_ptr; }; FOO(int val = 0) { std::fill( begin(), end(), val ); } iterator begin() { return m_data; } iterator end() { return m_data + 10; } explicit_iterator begin2() { return explicit_iterator( begin() ); } explicit_iterator end2() { return explicit_iterator( end() ); } private: int m_data[10]; }; int main (int, char *[]) { FOO foo; // This is the line that fails! (Not this one, the one right below!!!) std::copy( foo.begin(), --foo.end(), std::ostream_iterator&lt;int&gt;( std::cout, "\n" ) ); // C2105 // All these variants are good std::copy( foo.begin2(), --foo.end2(), std::ostream_iterator&lt;int&gt;( std::cout, "\n" ) ); // good std::copy( foo.begin(), foo.end() - 1, std::ostream_iterator&lt;int&gt;( std::cout, "\n" ) ); // good int* end = foo.end(); std::copy( foo.begin(), --end, std::ostream_iterator&lt;int&gt;( std::cout, "\n" ) ); // good return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload