c++ template seems to break access specifiers
The following code doesn't compile for obvious reasons, namely that Foo is trying to access a private member of Bar. However if you uncomment/comment the lines marked, making Foo a template, it does compile and outputs 42. What am I missing here? Why does this work? Seems to me it shouldn't.
Thanks for your help.
#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
}
If you are seeing this behavior, it is a compiler bug.
Both Comeau Online and Visual C++ 2010 reject the code as invalid because Bar::x
is inaccessible. g++ 4.1.2 incorrectly accepts the invalid code (someone would need to test with a later version to see if it's been fixed; that's the only version I have on this laptop).
这看起来像GCC bug 40843.它被列为UNCONFIRMED,但我也可以在g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3
上重现它。
VisualStudio 2010说:“错误C2248:'Bar :: x'[...]由于平台没有被指定,我估计这个假设几乎在Windows VC9上是错误的。
链接地址: http://www.djcxy.com/p/58708.html上一篇: 名称在typedef声明中是可选的?
下一篇: c ++模板似乎打破了访问说明符