不同版本的编译器对c++11的支持
不用看程序代码,只看错误就能发现问题。错误如下:
$ g++ csv.cpp main.cpp -o csv --std=11" title="c++11">c++11 -gIn file included from csv.cpp:1:0:
csv.hpp: In member function ‘void CSVReader::exchangeStream(const char*)’:
csv.hpp:54:13: error: ‘std::ifstream’ has no member named ‘swap’
m_backup.swap(m_is);
^
In file included from timer.hpp:8:0,
from main.cpp:1:
csv.hpp: In member function ‘void CSVReader::exchangeStream(const char*)’:
csv.hpp:54:13: error: ‘std::ifstream’ has no member named ‘swap’
m_backup.swap(m_is);
^
错误是,在std::ifstream里面找不到swap函数,但是这个函数确实在c++11里面能找到。
Public member functions(constructor)
Construct object and optionally open file (public member function )
open
Open file (public member function )
is_open
Check if a file is open (public member function )
close
Close file (public member function )
rdbuf
Get stream buffer (public member function )
operator=
Move assignment (public member function )
swap
Swap internals (public member function )
并且我编译程序的时候也确实加了-std=c++11选项,但是我在visual studio2019下就可以编译通过。那就肯定是编译器的原因了。
$ g++ -vUsing built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
我的g++版本是4.8.5在centos7.5,是能够支持c++11的,可见对g++对c++11支持的不完善;我在debian下用g++4.9.2的也同样编译不过;
然后我将g++版本切换到6.3.1发现就能编译通过了,情况如下:
root at localhost in /mnt/hgfs/share (master●●) $ scl enable devtoolset-6 bash
[root@localhost share]# zsh
root at localhost in /mnt/hgfs/share (master●●)
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-6/root/usr/libexec/gcc/x86_64-redhat-linux/6.3.1/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-6/root/usr --mandir=/opt/rh/devtoolset-6/root/usr/share/man --infodir=/opt/rh/devtoolset-6/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-plugin --with-linker-hash-style=gnu --enable-initfini-array --disable-libgcj --with-default-libstdcxx-abi=gcc4-compatible --with-isl=/builddir/build/BUILD/gcc-6.3.1-20170216/obj-x86_64-redhat-linux/isl-install --enable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 6.3.1 20170216 (Red Hat 6.3.1-3) (GCC)
root at localhost in /mnt/hgfs/share (master●●)
$ g++ csv.cpp main.cpp -o csv --std=c++11 -g
root at localhost in /mnt/hgfs/share (master●●)
$
可见低版本的g++可能对c++11支持的不完善。
以上是 不同版本的编译器对c++11的支持 的全部内容, 来源链接: utcz.com/z/513694.html