gpt4 book ai didi

c++ - 模板参数的访问限制

转载 作者:行者123 更新时间:2023-11-30 03:51:05 24 4
gpt4 key购买 nike

N4296::14.3/3 [temp.arg] 部分说:

The name of a template-argument shall be accessible at the point where it is used as a template-argument. [ Note: If the name of the template-argument is accessible at the point where it is used as a templateargument, there is no further access restriction in the resulting instantiation where the corresponding template-parameter name is used. —end note ]

好吧,让我们考虑下面的例子:

template <class T> class X{
static typename T::S ts;
};

class Y {
private:
class VS{ class S{ }; };
X<VS> x; //1
};

DEMO

//1 处实例化的模板 X 的模板参数是 class VS。现在,标准是这样说的:

The name of a template-argument shall be accessible at the point where it is used as a template-argument.

class VS 可在点//1 访问,因此满足要求。但尽管标准表示没有进一步的访问限制,但代码并未编译。

为什么代码没有编译通过?根据我提供的部分我可以说,它应该编译。

最佳答案

S 是私有(private)的,试试这个:

template <class T> class X{
static typename T::S ts;
};

class Y {
private:
class VS{ public: class S{ }; };
X<VS> x; //1
};

关于c++ - 模板参数的访问限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31488016/

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