开发者

decltype not working properly in 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'

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开发者_如何学Python.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) 


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.

In the meantime, you can use something like

#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 :)


According to wikipedia this is a known problem in the specification that simply has not been fixed yet. Due to this, a decltype cannot be part of a qualified-id.


According to latest standard, decltype-specifier is already allowed to do that, I believe you'll see GCC support in GCC 4.6.

qualified-id:
  ::opt nested-name-specifier unqualified-id
  :: identifier
  :: operator-function-id
  :: literal-operator-id
  :: template-id

nested-name-specifier:
  type-name ::
  namespace-name ::
  decltype-specifier ::
  nested-name-specifier identifier ::
  nested-name-specifier templateopt simple-template-id ::


It also doesn't work with GCC 4.5. See Wikipedia.


I don't have gcc 4.3.2 on my machine but I tested with gcc 4.4 and used std::remove_reference after a colleague of mine suggested it for a similar compile error.

$ g++-4.4 --version g++-4.4 (Ubuntu/Linaro 4.4.7-1ubuntu2) 4.4.7 Copyright (C) 2010 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

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

int main()
{ 
  int x = 5;
  decltype(x) y = 10;
  map<int, int> m;
  decltype(m) n;
  std::remove_reference<decltype(m)>::type::iterator it;
  std::cout << "Done"  << std::endl;
}

The command line output:

$ g++-4.4 -std=c++0x decltype.cpp 
$ ./a.out 
Done
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜