[英]How do you initialize a static member of an inaccessible template class?
我有類似下面的代碼,其中類模板有一個需要初始化的靜態變量。 然后我有一個模板類typedef,它使用私有的內部類作為模板參數:
template <typename T>
class Foo
{
private:
static const char* s_name;
};
class Bar
{
private:
class Baz
{
// ...
};
typedef Foo<Baz> FooBaz;
};
我以為我可以像這樣初始化靜態變量:
template<>
const char* Foo<Bar::Baz>::s_name = "foobaz";
它在MS Visual Studio 2015中有效。但是,當我使用clang構建時,我得到如下錯誤:
Error 'Baz' is a protected member of 'Bar'
為什么這適用於MSVS,但不是鏗鏘聲? 有沒有辦法初始化這個兼容的變量?
在“Bar”中公開“typedef Foo FooBaz”並使用“Bar :: FooBaz :: s_name”。
如果您不關心部分實例化,您可以隨時使用它。
template<typename T>
const char * Foo<T>::s_name = "foo";
對於局部人你需要讓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.