[英]How do you initialize a static member of an inaccessible template class?
I have code similar to the following, where a class template has a static variable that needs to be initialized. 我有类似下面的代码,其中类模板有一个需要初始化的静态变量。 I then have a template class typedef that uses a private, inner class as the template argument:
然后我有一个模板类typedef,它使用私有的内部类作为模板参数:
template <typename T>
class Foo
{
private:
static const char* s_name;
};
class Bar
{
private:
class Baz
{
// ...
};
typedef Foo<Baz> FooBaz;
};
I thought I could initialize the static variable like this: 我以为我可以像这样初始化静态变量:
template<>
const char* Foo<Bar::Baz>::s_name = "foobaz";
And it works... in MS Visual Studio 2015. However, when I build with clang, I get the an error like the following: 它在MS Visual Studio 2015中有效。但是,当我使用clang构建时,我得到如下错误:
Error 'Baz' is a protected member of 'Bar'
Why does this work with MSVS, but not clang? 为什么这适用于MSVS,但不是铿锵声? Is there a way to initialize this variable that will work with both?
有没有办法初始化这个兼容的变量?
在“Bar”中公开“typedef Foo FooBaz”并使用“Bar :: FooBaz :: s_name”。
You can always use that, if you do not care about partial instantiation. 如果您不关心部分实例化,您可以随时使用它。
template<typename T>
const char * Foo<T>::s_name = "foo";
for partials you need to make Foo a friend to Bar. 对于局部人你需要让Foo成为Bar的朋友。
class Bar
{
private:
class Baz {
// ...
};
friend class Foo<Baz>;
typedef Foo<Baz> FooBaz;
};
template<>
const char * Foo<Bar::Baz>::s_name = "barbaz";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.