2中不能正常工作

2中不能正常工作

本文介绍了decltype在gcc 4.3.2中不能正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include <map>
using namespace std;

int main()
{
    int x = 5;
    decltype(x) y = 10;
    map<int, int> m;
    decltype(m) n;
    decltype(m)::iterator it;
}

g++ -std=c++0x main.cpp

main.cpp: In function `int main()':
main.cpp:11: error: expected initializer before `it'

前两个decltypes工作。第三个结果导致编译器错误。这是这个gcc版本的问题吗?

The first 2 decltypes work. The third results in the compiler error. Is this a problem of this gcc version?

g++ -v
Using built-in specs.
Target: x86_64-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64 --enable-languages=c,c++,objc,fortran,obj-c++,java,ada --enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.3 --enable-ssp --disable-libssp --with-bugurl=http://bugs.opensuse.org/ --with-pkgversion='SUSE Linux' --disable-libgcj --disable-libmudflap --with-slibdir=/lib64 --with-system-zlib --enable-__cxa_atexit --enable-libstdcxx-allocator=new --disable-libstdcxx-pch --enable-version-specific-runtime-libs --program-suffix=-4.3 --enable-linux-futex --without-system-libunwind --with-cpu=generic --build=x86_64-suse-linux
Thread model: posix
gcc version 4.3.2 [gcc-4_3-branch revision 141291] (SUSE Linux)


推荐答案

使用decltype-expression来形成限定id( type :: type )的能力只是最近才刚刚(一年前) C ++ 0x工作纸,但它将成为完成标准的一部分。所以你写的代码实际上是良好的C ++ 0x。

The ability to use a decltype-expression to form a qualified-id (type::type) was only recently (well, a year ago), added to the C++0x working paper, but it will be part of the finished standard. So the code you have written is actually well-formed C++0x. Eventually gcc will catch up.

在此期间,您可以使用

#include <map>
template<typename T>
struct decltype_t
{
typedef T type;
};

#define DECLTYPE(expr) decltype_t<decltype(expr)>::type

int main()
{
 std::map<int, int> m;
 decltype(m) n;
 DECLTYPE(m)::iterator it; // works as expected
}

但是,如果你像我一样,会发现它很傻地不得不诉诸这样的诡计:)

Though, if you're like me, you'll find it silly having to resort to such tricks :)

这篇关于decltype在gcc 4.3.2中不能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:02