模板内部类中的 C++ decltype

标签 c++ templates inner-classes decltype

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

#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)))也不起作用。 其他更现代的编译器可以工作。

关于模板内部类中的 C++ decltype,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44677931/

相关文章:

c++ - 检查函数体是否为空

templates - 访问html模板golang中许多结构的 slice 中的结构变量

java - 这段代码是否存在额外的 NullPointerException 风险?

c++ - Win32 API 枚举 dll 导出函数?

c++ - 析构函数在哪里调用?

javascript - Knockout.js {{each}} 未在 javascript 模板中列出项目

Java:无法访问外部类的子类中嵌套类子类的 protected 方法

scala - 如何在 Scala 中构建合适的命名空间?

c++ - 如何设置用户只能输入 4 位代码的限制?

javascript - JS改变倒计时计数器