Wherever I read in the internet, it is strongly adviced that if I want my class to be working well with std::vector
(i.e. move semantics from my class were used by std::vector
) I should delcare move constructor as 'noexcept' ( or noexcept(true)
).
Why did std::vector
use it even though I marked it noexcept(false)
as an experiment?
#include <iostream>
#include <vector>
using std::cout;
struct T
{
T() { cout <<"T()\n"; }
T(const T&) { cout <<"T(const T&)\n"; }
T& operator= (const T&)
{ cout <<"T& operator= (const T&)\n"; return *this; }
~T() { cout << "~T()\n"; }
T& operator=(T&&) noexcept(false)
{ cout <<"T& operator=(T&&)\n"; return *this; }
T(T&&) noexcept(false)
{ cout << "T(T&&)\n"; }
};
int main()
{
std::vector<T> t_vec;
t_vec.push_back(T());
}
output:
T()
T(T&&)
~T()
~T()
Why ? What did I do wrong ?
Compiled on gcc 4.8.2 with CXX_FLAGS set to:
--std=c++11 -O0 -fno-elide-constructors