【问题标题】:decltype not working properly in gcc 4.3.2?decltype 在 gcc 4.3.2 中无法正常工作?
【发布时间】:2011-03-10 08:52:16
【问题描述】:
#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'

前 2 个 decltype 有效。第三个导致编译器错误。这是这个gcc版本的问题吗?

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) 

【问题讨论】:

  • 最新的稳定版是gcc 4.5.2,你试过了吗?

标签: c++ c++11 decltype


【解决方案1】:

使用 decltype-expression 形成限定 ID (type::type) 的能力只是最近(嗯,一年前)才被添加到 C++0x 工作文件中,但它将成为最终标准的一部分。所以你写的代码实际上是格式良好的 C++0x。最终 gcc 会迎头赶上。

与此同时,您可以使用类似的东西

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

不过,如果你像我一样,你会发现不得不诉诸这样的伎俩很愚蠢:)

【讨论】:

    【解决方案2】:

    根据wikipedia,这是规范中的一个已知问题,尚未修复。因此,decltype 不能成为 qualified-id 的一部分。

    【讨论】:

    • @nakiya:不确定。显然,对此的修复已在 2010 年 3 月的工作草案中被投票,但我不知道更多。
    【解决方案3】:

    根据最新的标准,decltype-specifier 已经被允许这样做了,我相信你会在 GCC 4.6 中看到对 GCC 的支持。

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

    【讨论】:

      【解决方案4】:

      它也不适用于 GCC 4.5。 See Wikipedia.

      【讨论】:

        【解决方案5】:

        我的机器上没有 gcc 4.3.2,但我使用 gcc 4.4 进行了测试,并在我的一位同事提出类似的编译错误建议后使用了 std::remove_reference

        $ g++-4.4 --version g++-4.4 (Ubuntu/Linaro 4.4.7-1ubuntu2) 4.4.7 版权所有 (C) 2010 Free Software Foundation, Inc. 这是免费的 软件;请参阅复制条件的来源。没有 保修单;甚至不适用于适销性或特定的适用性 目的。

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

        命令行输出:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多