【问题标题】:Visual C++ error with friend template class in C++11C++11 中友元模板类的 Visual C++ 错误
【发布时间】:2018-09-22 10:19:55
【问题描述】:

我有一段代码适用于 g++/clang++。最近有人向我报告它与 Visual C++ 冲突。

代码是这样的:

namespace q {
    template <typename X, typename Y>
    struct A {};
}

template <typename X>
struct B {
    template <typename Y>
    friend struct q::A;
};

int main() {
    return 0;
}

VC++ 返回以下错误:

source_file.cpp(9): error C2976: 'q::A': too few template arguments
source_file.cpp(3): note: see declaration of 'q::A'
source_file.cpp(10): note: see reference to class template instantiation 'B<X>' being compiled

谁是正确的?有便携的方法吗?

【问题讨论】:

    标签: c++ c++11 visual-c++ g++ clang++


    【解决方案1】:

    正确编写模板参数应该会有所帮助:

    template <typename X, typename Y>
    friend struct q::A;
    

    请注意,错误地将A 声明为朋友会使程序格式错误,不需要诊断。

    【讨论】:

    • 我希望实现的只是将q::A 声明为朋友,其中X 模板参数与封闭的B 类相同。我现在意识到我写的东西可能不是我想要的,我想要的可能是部分专业化,这是被禁止的。我是正确的,还是有办法做到这一点?
    猜你喜欢
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    相关资源
    最近更新 更多