函数模板在另一个类/命名空间中的专业化?
注意:这个问题与tinyxml只是松散相关,但是包含这样的细节可能有助于更好地说明这个概念。
我编写了一个函数模板,它将迭代父XML节点子节点,检索子元素,然后将该子元素值推送到向量。
“检索值”部分也被编写为函数模板:
即
template <typename Type>
Type getXmlCollectionItem(
const char* elementName, TiXmlNode* child, TiXmlNode* parent);
检索部分有专门化,用于返回不同类型的子元素值,例如 std::string 和其他自定义对象。
即,
template <>
std::string getXmlCollectionItem<std::string>(
const char* elementName, TiXmlNode* child, TiXmlNode* parent);
template <>
MyObject getXmlCollectionItem<MyObject>(
const char* elementName, TiXmlNode* child, TiXmlNode* parent);
这一切都运行得很好,但是令我震惊的是,在处理tinyxml 文件时,在共享函数库中使用这将非常有用。
问题:是否可以在一个命名空间中声明一个函数模板,例如命名空间UtilityFunctions
,它不了解'MyObject'
等特定对象类型,然后在其他命名空间中声明并定义该函数模板的专业化,这些命名空间确实了解特定对象类型,例如 'MyObject'
?
我的预感是这是不可能的,但是拥有通用函数模板的概念似乎对我来说足够有用,可以有一种替代方法来接近我正在寻找的功能...
如果任何术语不正确或解释不清楚,我深表歉意。我围绕这个主题做了很多研究(以达到在同一名称空间内工作函数模板专业化的目的),但尚未找到明确的答案。
NOTE: This question is only loosely related to tinyxml, however including details like that may help illustrate the concept better.
I have written a function template that will iterate through a parent XML nodes children, retrieve the value of the child element and then push that child element value to a vector.
The 'retrieve the value' part is also written as a function template:
i.e.
template <typename Type>
Type getXmlCollectionItem(
const char* elementName, TiXmlNode* child, TiXmlNode* parent);
There are specialisations for the retrieval part, for returning different types of child element value, e.g. std::string and other custom objects.
i.e.
template <>
std::string getXmlCollectionItem<std::string>(
const char* elementName, TiXmlNode* child, TiXmlNode* parent);
template <>
MyObject getXmlCollectionItem<MyObject>(
const char* elementName, TiXmlNode* child, TiXmlNode* parent);
This all works perfectly well, however it struck me that this would be very useful to have in a shared function library when dealing with tinyxml files.
Question: Is it possible to declare a function template in one namespace e.g. namespace UtilityFunctions
, which doesn't have any knowledge of specific object types like 'MyObject'
, and then declare and define specialisations of that function template in other namespaces which do have the knowledge of specific object types like 'MyObject'
?
My hunch is that it isn't possible, but the concept of having a common function template seems to me to be useful enough for there to be an alternative way of getting close to the functionality I'm looking for...
Apologies if any of the terminology is incorrect or explanation is unclear. I've done a lot of research around this topic (to get to the point of working function template specialisation within the same namespace) but found no definitive answer as yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不可能在一个命名空间中编写在另一个命名空间中定义的模板的特化(因为这不是该模板的特化,在另一个命名空间中定义它会是一个不同的模板)。
然而,扩展最初定义模板的名称空间是完全可以的,将您的专业化写入完全独立的源文件中。
因此,这是您不能做的事情:
您可以在 http 上看到上述内容的不错的错误消息://www.comeaucomputing.com/tryitout/
这是您可以做的事情:
有什么原因会导致这个问题吗?
此外,如果您将工作委托给与您正在读取的对象关联的函数(成员函数或自由函数),则可以依赖通过 ADL 找到并调用该函数。这意味着您应该能够最大程度地减少上述此类专业化的数量。
这是示例:
编辑以添加示例
It is not possible to write in one namespace a specialization of a template defined in another namespace (since that would not be specialization of that template, being defined in another namespace it would be a different template).
However it is perfectly OK to extend namespace where template has been originally defined, writing your specialization in totally separate source file.
So here is what you cannot do:
You can see nice error message for the above at http://www.comeaucomputing.com/tryitout/
Here is what you can do:
Is there any reason why that would be a problem?
Also, if you delegate the work to a function associated with the object you are reading (either member or free function), you can depend on that function being found via ADL and called. Meaning you should be able to minimize the amount of such specializations like the above.
Here is example:
Edited to add an example
这里的要点是“模板的每个声明都必须放置在同一名称空间中,就像任何其他命名实体的重复声明一样”
在不同的名称空间中声明/定义它是无效的,
有关更多信息,请参阅常见问题解答中的第12点
Point here is " Every declaration for a template must be placed in the same namespace, just like repeated declarations of any other named entity "
declaration/defining it in different namespace is not valid,
for more info please go through the 12th point in FAQ