【问题标题】:C++ decltype in template inner class模板内部类中的 C++ decltype
【发布时间】:2017-06-21 13:44:37
【问题描述】:

下面的例子说明了我的问题:

#include <vector>

    template <class T>
    class TestNest{
    public:
        std::vector<T> m_list;
        class InsideNest{
            const TestNest<T>* m_test;
            decltype(m_test->m_list.begin()) m_iter;
        public:
            InsideNest(const TestNest<T>* source)
                :m_test(source)
                ,m_iter(source->m_list.begin())
            {}
        };
    };

int main(int argc, char *argv[])
{
    TestNest<int> outside;
    TestNest<int>::InsideNest inside(&outside);
}

无法编译的部分(至少在 MSVC2013 中没有)是decltype(m_test-&gt;m_list.begin())。知道如何解决这个问题吗?

编辑:更改代码以显示 main() 和 #include

【问题讨论】:

  • 你得到什么编译器错误?
  • 错误 C2227:'->m_list' 的左侧必须指向类/结构/联合/通用类型
  • 在 VS2015 中一切正常。请显示包含。
  • 另外,我忘了说我不能只使用std::vector&lt;T&gt;::iterator,因为我的真实课程比上面的更复杂
  • GCC (minGW) 无怨无悔地接受 - 在修复 m_iter(source-&gt;m_list.begin()) 之后

标签: c++ templates inner-classes decltype


【解决方案1】:

关闭问题。这是MSVC2013的一个缺点。它将在“计算”完整类型的成员之前解析decltype(),因此在 decltype 内部对方法的任何访问都是编译器错误。 即使使用全局模板函数(例如decltype(std::begin(m_list)))也不起作用。 其他更现代的编译器也可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    相关资源
    最近更新 更多