【发布时间】:2015-12-22 19:56:15
【问题描述】:
在 C++14 应用程序的上下文中,我使用的方案可以恢复如下(最小可重复测试):
template <class Container>
struct LocateFunctions {
auto get_it() const // <-- here is the problem
{
auto ret = typename Container::Iterator();
return ret;
}
};
template <typename T>
struct A : public LocateFunctions<A<T>> {
struct Iterator {};
};
int main() {
A<int> a;
}
这种方法可以在 C++14 中完美地编译和运行,使用 GCC 和 Clang 编译器。
现在我想将我的应用程序迁移到 Windows,为此我正在使用 MinGW。不幸的是,它的最新版本带来了 GCC 4.9,它不能编译 C++14。这似乎不是一个严重的问题,因为我可以在 C++11 中重写 C++14 结构。所以,我重写get_it()方法如下:
typename Container::Iterator get_it() const
{
auto ret = typename Container::Iterator();
return ret;
}
不幸的是它没有编译。两种编译器都会产生以下错误:
error: no type named ‘Iterator’ in ‘struct A<int>’
typename Container::Iterator get_it() const
^
我也试过了:
auto get_it() const -> decltype(typename Container::Iterator())
{
auto ret = typename Container::Iterator();
return ret;
}
但我得到完全相同的错误。
由于两个编译器无法识别返回的类型,我想不可能确定它。但我真的不知道为什么。
有人可以解释一下为什么不编译并最终在 C++11 中进行重构的方法可以编译吗?
【问题讨论】:
标签: c++ templates c++11 c++14 name-lookup