模板化赋值运算符:有效的 C++?
只是一个快速而简单的问题,但在任何文档中都找不到它。
template <class T>
T* Some_Class<T>::Some_Static_Variable = NULL;
它用 g++ 编译,但我不确定这是否有效。 是吗?
Just a quick and simple question, but couldn't find it in any documentation.
template <class T>
T* Some_Class<T>::Some_Static_Variable = NULL;
It compiles with g++, but I am not sure if this is valid usage. Is it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这段代码是正确的。 有关详细信息,请参阅此 C++ 模板教程
http ://www.is.pku.edu.cn/~qzy/cpp/vc-stl/templates.htm#T14
Yes this code is correct. See this C++ Templates tutorial for more information
http://www.is.pku.edu.cn/~qzy/cpp/vc-stl/templates.htm#T14
这是有效的 C++,但它与模板化赋值运算符无关?! 该代码段定义了
SomeClass
的静态成员,并将其初始值设置为NULL
。 只要您只执行一次就可以了,否则您就会踩到可怕的一个定义规则
。模板赋值运算符类似于:
模板赋值运算符对于在实现类变体时提供转换最有用。 如果您要使用这些生物,则应该考虑很多注意事项。 Google 搜索将会发现有问题的案例。
That is valid C++ but it has nothing to do with a templated assignment operator?! The snippet defines a static member of
SomeClass<T>
and sets its initial value toNULL
. This is fine as long as you only do it once otherwise you step on the dreadedOne Definition Rule
.A templated assignment operator is something like:
The template assignment operator is most useful for providing conversions when implementing variant-like classes. There are a bunch of caveats that you should take into consideration if you are going to use these critters though. A Google search will turn up the problematic cases.