首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++11自动变量

C++11自动变量
EN

Stack Overflow用户
提问于 2015-07-10 12:05:24
回答 2查看 368关注 0票数 1

现代C++风格的要点(视频)中,Herb倡导:

代码语言:javascript
复制
auto varname = Constructor{ ... };

而不是

代码语言:javascript
复制
Constructor varname( ... );

我试着做:

代码语言:javascript
复制
auto log = fstream{"log.txt", fstream::out};

但是从g++4.8 -std=c++11收到了一条关于已删除函数的错误消息(不管我是使用大括号还是括号)。

是我的错还是g++的错?

代码语言:javascript
复制
fstream log{"log.txt", fstream::out};

效果很好。

MCVE:

代码语言:javascript
复制
#include <fstream>

using namespace std;
int main(int argc, char **argv)
{
    auto log = fstream{"log.txt", fstream::out};
    //fstream log{"log.txt", fstream::out};
    log << "hello world" << endl;
}

错误:

代码语言:javascript
复制
g++ -std=c++11    1.cc   -o 1
1.cc: In function ‘int main(int, char**)’:
1.cc:6:47: error: use of deleted function ‘std::basic_fstream<char>::basic_fstream(const std::basic_fstream<char>&)’
     auto log = fstream{"log.txt", fstream::out};
                                               ^
In file included from 1.cc:1:0:
/usr/include/c++/4.8/fstream:776:11: note: ‘std::basic_fstream<char>::basic_fstream(const std::basic_fstream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
     class basic_fstream : public basic_iostream<_CharT, _Traits>
           ^
/usr/include/c++/4.8/fstream:776:11: error: use of deleted function ‘std::basic_iostream<char>::basic_iostream(const std::basic_iostream<char>&)’
In file included from /usr/include/c++/4.8/fstream:38:0,
                 from 1.cc:1:
/usr/include/c++/4.8/istream:795:11: note: ‘std::basic_iostream<char>::basic_iostream(const std::basic_iostream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
     class basic_iostream
           ^
/usr/include/c++/4.8/istream:795:11: error: use of deleted function ‘std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)’
/usr/include/c++/4.8/istream:58:11: note: ‘std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
     class basic_istream : virtual public basic_ios<_CharT, _Traits>
           ^
/usr/include/c++/4.8/istream:58:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
In file included from /usr/include/c++/4.8/ios:44:0,
                 from /usr/include/c++/4.8/istream:38,
                 from /usr/include/c++/4.8/fstream:38,
                 from 1.cc:1:
/usr/include/c++/4.8/bits/basic_ios.h:66:11: note: ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ is implicitly deleted because the default definition would be ill-formed:
     class basic_ios : public ios_base
           ^
In file included from /usr/include/c++/4.8/ios:42:0,
                 from /usr/include/c++/4.8/istream:38,
                 from /usr/include/c++/4.8/fstream:38,
                 from 1.cc:1:
/usr/include/c++/4.8/bits/ios_base.h:786:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
     ios_base(const ios_base&);
     ^
In file included from /usr/include/c++/4.8/ios:44:0,
                 from /usr/include/c++/4.8/istream:38,
                 from /usr/include/c++/4.8/fstream:38,
                 from 1.cc:1:
/usr/include/c++/4.8/bits/basic_ios.h:66:11: error: within this context
     class basic_ios : public ios_base
           ^
In file included from /usr/include/c++/4.8/fstream:38:0,
                 from 1.cc:1:
/usr/include/c++/4.8/istream:795:11: error: use of deleted function ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’
     class basic_iostream
           ^
In file included from /usr/include/c++/4.8/istream:39:0,
                 from /usr/include/c++/4.8/fstream:38,
                 from 1.cc:1:
/usr/include/c++/4.8/ostream:58:11: note: ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
     class basic_ostream : virtual public basic_ios<_CharT, _Traits>
           ^
/usr/include/c++/4.8/ostream:58:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
In file included from /usr/include/c++/4.8/fstream:38:0,
                 from 1.cc:1:
/usr/include/c++/4.8/istream:795:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
     class basic_iostream
           ^
In file included from 1.cc:1:0:
/usr/include/c++/4.8/fstream:776:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
     class basic_fstream : public basic_iostream<_CharT, _Traits>
           ^
/usr/include/c++/4.8/fstream:776:11: error: use of deleted function ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’
/usr/include/c++/4.8/fstream:72:11: note: ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’ is implicitly deleted because the default definition would be ill-formed:
     class basic_filebuf : public basic_streambuf<_CharT, _Traits>
           ^
In file included from /usr/include/c++/4.8/ios:43:0,
                 from /usr/include/c++/4.8/istream:38,
                 from /usr/include/c++/4.8/fstream:38,
                 from 1.cc:1:
/usr/include/c++/4.8/streambuf:802:7: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’ is private
       basic_streambuf(const basic_streambuf& __sb)
       ^
In file included from 1.cc:1:0:
/usr/include/c++/4.8/fstream:72:11: error: within this context
     class basic_filebuf : public basic_streambuf<_CharT, _Traits>
           ^
make: *** [1] Error 1
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-10 12:21:23

auto var = Type{...};语法要求Type必须是可移动的,但是gcc直到GCC 5.1才实现可移动的fstreams。你的代码用GCC 5.1编译

票数 5
EN

Stack Overflow用户

发布于 2015-07-10 12:13:27

如果我没有错的话,这个建议来自赫伯·萨特。

显然,问题在于你不能复制构造std::fstream (std::fstream的复制构造函数被标记为delete),然而,直觉地说,由于右边有一个临时构造函数,所以应该使用移动构造函数而不是复制构造函数。

实际上,以下内容在clang上编译得很好(clang++ -std=c++11,OS ):

代码语言:javascript
复制
#include <iostream>
#include <fstream>

int main() {

auto log = std::fstream{"test.cpp", std::fstream::out};

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31340345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档