c++ - 使用 typealias 代替定义中类中定义的 typedef

标签 c++ gcc compiler-bug

为什么编译失败?我试图减少重复的次数,因为在实际代码中类名很大,type 实际上是一个很长的名字

https://godbolt.org/z/8YarWs

#include <vector>

template<typename T>
struct S {
    using type = std::vector<T>;

    type f() const;
};

template<typename T>
using type_t = typename S<T>::type;

template <typename T>
type_t<T> S<T>::f() const { }

上面的代码失败如下

<source>:17:14: error: no declaration matches 'type_t<T> S<T>::f() const'
   17 | type_t < T > S < T >::f() const
      |              ^~~~~~~
<source>:8:10: note: candidate is: 'S<T>::type S<T>::f() const'
    8 |     type f() const
      |          ^
<source>:4:8: note: 'struct S<T>' defined here
    4 | struct S
      |        ^

最佳答案

这是一个 GCC 错误;最近的(1) 已确认打开错误报告似乎是:

  • Bug 69348:别名声明不能在声明符的限定符内使用

这突出表明 GCC 拒绝以下格式良好的程序:

template <class T>
struct X {
    int foo();
};

template <class T>
using foo2 = X<T>;

template <class T>
int foo2<T>::foo()
{
}

错误信息

error: invalid use of incomplete type 'struct X<T>'

(1) 扫描 GCC:s bugzilla,似乎有许多与别名模板相关的“拒绝有效”(公开/未确认)错误报告,在大多数报告中,据报道 clang 接受(可以说很好-形成)示例程序。

关于c++ - 使用 typealias 代替定义中类中定义的 typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65110143/

相关文章:

c++ - 为什么 C++ 的创建者决定使用构造函数初始化列表来初始化基类?

c++ - 是否存在绝对需要 typedef 的情况?

c++ - 是 Visual C++ 优化器错误还是我的代码中有错误?

visual-c++ - 64 位指针减法、有符号整数下溢和可能的编译器错误?

c - 为什么 "unused attribute"为结构数组生成警告?

c++ - MSVC++ 中可能的编译器错误

android - Cocos2d-x crash due to ccLayer->schedule(schedule_selector(...), ...);

c++ - MSVC 中的地址清理程序 : why does it report an error on startup?

c++ - 公共(public)和私有(private)领域的内存分配 - GCC方式

macos - macOS Mojave 上的 gcc 损坏