Jump to content
  • Advertisement
Sign in to follow this  
wintertime

weird dependence of std::vector on extraneous declaration

This topic is 2145 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is a shortened testcase I wrote after I got this problem in more complicated real code:

#include <vector>
#include <iostream>

class Thing {
public:
  explicit Thing(int n_) :
    n(n_)
  {
  }
  Thing(Thing&& t) noexcept :
    n(t.n)
  {
    t.n=0;
  }
  const Thing& operator=(Thing&& t) noexcept {
    if(this!=&t) {
      n=t.n;
      t.n=0;
    }
    return *this;
  }
  ~Thing() {
  }
  int Get() const {
    return n;
  }
private:
  int n;

  Thing(const Thing& t);
  const Thing& operator=(const Thing& t);
};

int main() {
  std::vector<Thing> v;
  v.push_back(Thing(1));
  std::cout << v[0].Get();
}

If I comment out the declaration of the copy constructor or add =delete it compiles and runs flawlessly.

With the added declaration I get a compiler error (using mingw gcc 4.7.2) for push_back deep inside many levels of templated type traits, because the copy contructor is private, although it does not really need it because the move constructor is there.

Does anyone know if its just a problem with the headers/compiler, if its a problem of C++11 or its just a disallowed cornercase?

(NB I read on a blog about C++11 that the noexcept would help to allow the container to use the move constructor, but it didnt help.)

Share this post


Link to post
Share on other sites
Advertisement

It seems that this is a problem with the compiler. Clang 3.2 compiles this fine for me. This also compiles fine for GCC 4.8.1. It's worth noting that GCC didn't fully implement C++11 until v4.8.1 (with the possibility of bugs always existing, and the exception of garbage collection support).

 

Two notes: First, you should just mark them as = delete if you really aren't going to define them. Second, operator= should probably return Thing& instead of const Thing&, seeing as that's the "normal" return type of operator=.

Share this post


Link to post
Share on other sites

Thank you for taking the time to test it.

I will look into updating the compiler (had waited for the slow official mingw to get 4.8).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!