【问题标题】:C++11 auto variable [duplicate]C ++ 11自动变量[重复]
【发布时间】:2015-07-10 12:05:24
【问题描述】:

Essentials of Modern C++ Style (video),Herb Sutter 倡导者:

auto varname = Constructor{ ... };

而不是

Constructor varname( ... );

我试过了:

auto log = fstream{"log.txt", fstream::out};

但收到来自g++4.8 -std=c++11 的关于已删除函数的错误消息(无论我使用的是大括号还是圆括号)。

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

fstream log{"log.txt", fstream::out};

工作正常。

MCVE:

#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;
}

错误:

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

【问题讨论】:

  • 请发布错误信息。
  • 请提供Minimal, Complete, and Verifiable example 和完整的错误信息。
  • 您可能指的是 Herb Sutter 的建议。但是,fstream 的复制构造函数是delete,所以这可能是问题所在。
  • 请注意,如果您要使用 C++ 版本特定标签,如 C++11,在这种情况下,您还应该包括主标签 C++。
  • 顺便说一下,out 等常量实际上是在ios 中声明的,因此您可以将它们引用为ios::out 而不是fstream::out...虽然它可能没有真的很重要。

标签: c++ c++11


【解决方案1】:

auto var = Type{...}; 语法要求 Type 是可移动构造的,但 gcc 直到 GCC 5.1 才实现可移动 fstream。你的代码compiles with GCC 5.1

【讨论】:

    【解决方案2】:

    如果我没记错的话,这个建议来自 Herb Sutter。

    显然,问题是由于您无法复制构造std::fstreamstd::fstream 的复制构造函数标记为delete)。然而,直觉上有人会说,因为你在右手边有一个临时的,所以应该使用移动构造函数而不是复制构造函数。

    事实上,以下在 clang 上编译得很好(clang++ -std=c++11,OS X Mavericks):

    #include <iostream>
    #include <fstream>
    
    int main() {
    
    auto log = std::fstream{"test.cpp", std::fstream::out};
    
    }
    

    【讨论】:

    • 关于你的第一句话:是的,确实,他为此创造了“AAA”(几乎总是自动)这个词。见herbsutter.com/2013/08/12/…。然而,据我所知,这种风格还没有被广泛采用。就个人而言,我不确定它的优缺点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 2014-03-15
    相关资源
    最近更新 更多