作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下代码无法编译(使用 clang):
template<int N>
class Foo {
public:
Foo() : value(N) { }
void getValue(Foo<1>& foo)
{
value = foo.value;
}
protected:
int value;
};
int main(int argc, const char * argv[])
{
Foo<1> fooOne = Foo<1>();
Foo<2> fooTwo = Foo<2>();
fooTwo.getValue(fooOne);
return 0;
}
错误是main.cpp:21:15: error: 'value' is a protected member of 'Foo<1>'
.这一切都很好。
我的问题是有没有办法使用 friend 让它工作?例如,以下代码会产生相同的错误,但我希望它会起作用。
template<int N>
class Foo {
public:
Foo() : value(N) { }
friend class Foo<1>;
void getValue(Foo<1>& foo)
{
value = foo.value;
}
protected:
int value;
};
我当然可以非常可怕并使用 Accessing protected member of template parameter 中的技巧或 http://www.gotw.ca/gotw/076.htm .但我宁愿不诉诸那种级别的 hackery 来做一些我可能只是很感兴趣的事情。
最佳答案
你是 friend
- 错误的方式。这是Foo<N>
需要成为 Foo<1>
的 friend ,因为它需要访问 Foo<1>
的内部结构;你正在制作Foo<1>
一个friend
的 Foo<N>
.为简单起见,您可以只使用 friend
所有这些:
template <int N>
class Foo {
// mass inter-Foo friendship
template <int > friend class Foo;
// rest as you had before
};
关于c++ - 非特化模板中模板特化的成员访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30338902/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
正如您在 this travis.yml 中看到的那样文件,我的代码依赖于一些第三方库,我在构建项目之前将它们安装在远程系统上。 Travis 每次推送提交时都会下载并构建这些库,这可以避免吗?我的意
我是一名优秀的程序员,十分优秀!