gpt4 book ai didi

c++ - 嵌套模板类: Parameter default value not accepted

转载 作者:行者123 更新时间:2023-12-02 07:21:45 25 4
gpt4 key购买 nike

编译:

class A {
public:
template <int, int> class B;
};

template <int y, int z = y>
class A::B {
};

int main() {}

这不是:

template <int x>
class A {
public:
template <int, int> class B;
};

template <int x>
template <int y, int z = y>
class A<x>::B {
};

int main() {}

g++ main.cpp 说:(版本 9.1.0)

main.cpp:24:13: error: default argument for template parameter for class enclosing ‘class A<x>::B<<anonymous>, <anonymous> >’
24 | class A<x>::B {
| ^

出了什么问题?

最佳答案

默认参数需要在声明中:

template <int x>
class A {
public:
template <int y, int = y> class B;
};

template <int x>
template <int y, int z>
class A<x>::B {
};

int main() {
A<1>::B<2> b;
}

Default parameters are not allowed in the out-of-class definition of a member of a class template (they have to be provided in the declaration inside the class body). Note that member templates of non-template classes can use default parameters in their out-of-class definitions (see GCC bug 53856)

( https://en.cppreference.com/w/cpp/language/template_parameters )

关于c++ - 嵌套模板类: Parameter default value not accepted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58292829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com