以下代码由于显而易见的原因而无法编译,即Foo正在尝试访问Bar的私有成员.但是,如果您取消注释/注释标记的行,使Foo成为模板,它会编译并输出42.我在这里缺少什么?为什么这样做?在我看来它不应该.
谢谢你的帮助.
#include <iostream>
class Bar {
private:
static const int x = 42;
};
//template <int> // uncomment me
struct Foo {
static const int i = Bar::x;
};
int main(int argc, char* argv[]) {
std::cout << Foo::i << std::endl; // comment me
//std::cout << Foo<0>::i << std::endl; // uncomment me
}
Run Code Online (Sandbox Code Playgroud)