为什么编译失败?我试图减少重复的次数,因为在实际代码中类名很大,type
实际上是一个很长的名字
#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/