官术网_书友最值得收藏!

Pitfalls with non-noexcept move constructors

Recall the implementation of vector::resize() from section Resizing a std::vector. When the vector resizes, it reallocates its underlying array and moves its elements into the new array--unless the element type is not "nothrow move-constructible," in which case it copies its elements! What this means is that resizing a vector of your own class type will be unnecessarily "pessimized" unless you go out of your way to specify that your move constructor is noexcept.

Consider the following class definitions:

    struct Bad {
int x = 0;
Bad() = default;
Bad(const Bad&) { puts("copy Bad"); }
Bad(Bad&&) { puts("move Bad"); }
};

struct Good {
int x = 0;
Good() = default;
Good(const Good&) { puts("copy Good"); }
Good(Good&&) noexcept { puts("move Good"); }
};

class ImplicitlyGood {
std::string x;
Good y;
};

class ImplicitlyBad {
std::string x;
Bad y;
};

We can test the behavior of these classes in isolation using a test harness such as the following. Running test() will print "copy Bad--move Good--copy Bad--move Good." What an appropriate mantra!

    template<class T>
void test_resizing()
{
std::vector<T> vec(1);
// Force a reallocation on the vector.
vec.resize(vec.capacity() + 1);
}

void test()
{
test_resizing<Good>();
test_resizing<Bad>();
test_resizing<ImplicitlyGood>();
test_resizing<ImplicitlyBad>();
}

This is a subtle and arcane point, but it can have a major effect on the efficiency of your C++ code in practice. A good rule of thumb is: Whenever you declare your own move constructor or swap function, make sure you declare it noexcept.

主站蜘蛛池模板: 新野县| 仪征市| 尉犁县| 故城县| 明溪县| 乳山市| 泗阳县| 焉耆| 珠海市| 浮梁县| 杨浦区| 开鲁县| 汉中市| 凭祥市| 嘉荫县| 股票| 依安县| 务川| 三门峡市| 铜山县| 漠河县| 疏附县| 夏津县| 嘉黎县| 绍兴县| 凤山县| 故城县| 濉溪县| 永福县| 涞水县| 新建县| 开阳县| 乌兰县| 临漳县| 屏南县| 弥渡县| 黄大仙区| 江陵县| 光山县| 福鼎市| 招远市|