仅当我将-DA=1标志传递给编译器时,以下程序才会编译:
#include <iostream>
#include <vector>
#include <algorithm>
struct element {
element() = default;
element(element&&) = default;
element& operator=(element&&) = default;
element(const element&) = delete;
element& operator=(const element&) = delete;
#if A
std::vector<int> v;
#endif
};
int main() {
std::vector<element> source(10), destination;
std::move(std::begin(source), std::end(source), std::back_inserter(destination));
}如果传递了-DA=0,则编译将失败,并显示以下错误:
stl_algobase.h:373:4: error: static assertion failed: type is not assignableCheck it out on Coliru.
使用GCC 4.9或Clang 3.4失败。
成员变量的存在是否会影响显式默认构造函数的行为?
更新
我在使用stdlibc++的时候,同时使用了stdlibc++的两个版本。代码在使用Clang3.4和libc++时进行编译。
发布于 2014-07-09 09:39:58
此版本的std::move为:
template<class InputIterator, class OutputIterator>
OutputIterator move(InputIterator first, InputIterator last,
OutputIterator result);OutputIterator属性由output.iterators定义,特别是以下表达式必须有效:
*r = o其中r是输出迭代器。
根据Coliru显示的错误消息,似乎库正在检查std::is_copy_assignable<element>,这当然是假的。
这似乎是一个错误;移动应该使用移动赋值操作符。
在相同的Coliru中比较以下内容:
int main() {
std::vector<element> e(1);
std::vector<element> d(1);
*e.begin() = std::move( *d.begin() ); // A=1 OK A=0 OK
std::move(d.begin(), d.end(), e.begin()); // A=1 OK A=0 errors
}std::move (3个参数)的定义包括它对每个索引执行*(result + n) = std::move( *(first + n) );。因此,如果我的第一行有效,那么我的第二行也应该有效。
https://stackoverflow.com/questions/24643844
复制相似问题