是否隐式删除了 ifsteam 的移动构造函数
Is the move constructor of ifsteam implicitly deleted?
我有以下简单的类:
class Source
{
public:
Source() = default;
Source(Source const&) = delete;
Source(Source&&) = default;
explicit Source(std::string const& fileName)
: inputStream(fileName), path_(fileName)
{}
~Source() = default;
auto path() const -> std::string
{
return this->path_;
}
std::ifstream inputStream;
private:
std::string path_;
};
auto main(int argc, char* argv[]) -> int
{
Source source(Source("test.txt"));
cout << source.path() << "n";
return 0;
}
根据 cppreference ifstream
有一个move
构造函数,但是当我尝试使用 MinGW 4.7.2
编译它时,我收到以下错误:
..\src\main.cpp:32:46:错误:使用已删除的功能 'cy::Source::Source(cy::Source&&)' 在包含自的文件中 ..\src\main.cpp:10:0: source.hpp:28:5: 注意: "cy::Source::Source(cy::Source&&)"被隐式删除,因为 默认定义格式不正确:源.hpp:28:5:错误:使用 删除函数 'std::basic_ifstream::basic_ifstream(const 标准::basic_ifstream&)' C:\mingw\bin../lib/gcc/mingw32/4.7.2/include/c++/fstream:420:11: 注意:"std::basic_ifstream::basic_ifstream(const std::basic_ifstream&)' 被隐式删除,因为默认值 定义格式不正确: C:\mingw\bin../lib/gcc/mingw32/4.7.2/include/c++/fstream:420:11: 错误:使用已删除的功能 'std::basic_istream::basic_istream(const 标准::basic_istream&)'
我做错了什么吗?还是 cpp偏好的文档不准确?还是 GCC 4.7.2 有错误?
事实证明,GCC 的标准库实现尚未实现流类的移动和交换操作。有关 gcc 标准库中 C++11 要素的当前状态的详细信息,请参阅此处。
感谢杰西·古德提供信息和链接。
我意识到这个答案有点晚了,但要给出一个不可移动的类移动语义,你可以写一个非常简单的包装类。例如:
#include <memory>
template <typename T>
class swap_wrapper
{
//! internal buffer to hold object (on heap)
std::unique_ptr<T> p_obj;
public:
//! allows any of objects constructors to be called directly
template <typename... Args>
explicit swap_wrapper(Args&&... args)
: p_obj(
new T( std::forward<Args>(args)... )
) { }
//! swaps pointer value of T object is unchanged therefore this
//! function puts no requirement on base class.
//! note p_obj is left as a nullptr so dereferencing will cause
//! undefined behaviour.
swap_wrapper (swap_wrapper &&other) noexcept
: p_obj(nullptr)
{
swap(other);
}
//! swaps pointer like above,
//! T object is not touched; just the pointer.
swap_wrapper &operator = (swap_wrapper &&other) noexcept
{
swap(other);
return *this;
}
//! uses swap member function of std:unique_ptr
void swap(swap_wrapper &other) noexcept
{
std::swap(p_obj, other.p_obj);
}
//! operators to allow access to stream
T& operator *() { return *p_obj; }
T const& operator *() const { return *p_obj; }
T * operator ->() { return p_obj.get(); }
T const * operator ->() const { return p_obj.get(); }
};
//! overload of default swap (calls member function swap)
template <typename S>
inline void swap(swap_wrapper<S> &one, swap_wrapper<S> &two) noexcept
{ one.swap(two); }
然后可以从函数返回此包装器,作为右值参数传递等。
- 为什么不调用移动构造函数?(默认情况下只有构造器,没有别的)
- std::vector::p ush_back() 不会在 MSVC 上编译具有已删除移动构造函数的对象
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- 为什么调用复制构造函数而不是移动构造函数?
- 基类中的默认析构函数禁用子类中的移动构造函数(如果有成员)
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- 移动构造函数和右值引用
- 为什么 std::memmove 中联合的默认非平凡移动构造函数C++?
- 具有专用化的模板类中的可靠条件复制和移动构造函数
- C++:为什么不调用移动构造函数?
- 移动构造函数永远不会被调用
- C++:关于使用 Stroustrup 示例移动构造函数/赋值的问题
- 运算符+ 的规范实现涉及额外的移动构造函数
- C ++为什么在移动构造函数中需要移动/前进
- 为什么在删除"移动构造函数"时使用"复制构造函数"?
- 为什么这里不调用移动构造函数?
- 隐式移动构造函数
- 如何为具有私有成员的派生类实现移动构造函数
- 为什么不调用移动构造函数
- 是否可以避免在以下代码中复制/移动构造函数的需要?