C++模板内部类中的 decltype

C++ decltype in template inner class

本文关键字:decltype 内部类 C++      更新时间:2023-10-16

我的问题由以下示例说明:

#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->m_list.begin()).知道我该如何解决这个问题吗?

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

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