模板类成员专门化的声明 [英] Declaration of template class member specialization

查看:151
本文介绍了模板类成员专门化的声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在模板类中专门化一个(静态)成员函数/常量时,我​​很难理解声明的含义。

When I specialize a (static) member function/constant in a template class, I'm confused as to where the declaration is meant to go.

以下是我要做什么的示例 - 直接从 IBM对模板专业化的参考

Here's an example of what I what to do - yoinked directly from IBM's reference on template specialization:

=== IBM会员专业化示例===

===IBM Member Specialization Example===

template<class T> class X {
public:
   static T v;
   static void f(T);
};

template<class T> T X<T>::v = 0;
template<class T> void X<T>::f(T arg) { v = arg; }

template<> char* X<char*>::v = "Hello";
template<> void X<float>::f(float arg) { v = arg * 2; }

int main() {
   X<char*> a, b;
   X<float> c;
   c.f(10); // X<float>::v now set to 20
}

,我如何将它分为header / cpp文件?通用的实现显然在标题中,但是专业化呢?

The question is, how do I divide this into header/cpp files? The generic implementation is obviously in the header, but what about the specialization?

它不能进入​​头文件,因为它具体,导致多重定义。但是如果它进入.cpp文件,是调用X :: f()知道专门化的代码,或者它可能依赖于通用的X :: f()?

It can't go in the header file, because it's concrete, leading to multiple definition. But if it goes into the .cpp file, is code which calls X::f() aware of the specialization, or might it rely on the generic X::f()?

到目前为止,我只有在.cpp中的专业化,在标题中没有声明。我没有编译或甚至运行我的代码(在gcc,不记得版本在现在),而且它的行为像预期的 - 识别专业化。但A)我不知道这是正确的,我想知道是什么,和B)我的Doxygen文档出来了,并且非常误导(更多的是在一会儿问题)。

So far I've got the specialization in the .cpp only, with no declaration in the header. I'm not having trouble compiling or even running my code (on gcc, don't remember the version at the moment), and it behaves as expected - recognizing the specialization. But A) I'm not sure this is correct, and I'd like to know what is, and B) my Doxygen documentation comes out wonky and very misleading (more on that in a moment a later question).

对我来说最自然的是类似这样的东西,声明头文件中的特殊化并在.cpp中定义它:

What seems most natural to me would be something like this, declaring the specialization in the header and defining it in the .cpp:

=== XClass.hpp ===

===XClass.hpp===

#ifndef XCLASS_HPP
#define XCLASS_HPP

template<class T> class X {
public:
   static T v;
   static void f(T);
};

template<class T> T X<T>::v = 0;
template<class T> void X<T>::f(T arg) { v = arg; }

/* declaration of specialized functions */
template<> char* X<char*>::v;
template<> void X<float>::f(float arg);

#endif

=== XClass.cpp ===



===XClass.cpp===

#include <XClass.hpp>

/* concrete implementation of specialized functions */
template<> char* X<char*>::v = "Hello";
template<> void X<float>::f(float arg) { v = arg * 2; }

...但我不知道这是否正确。任何想法?

...but I have no idea if this is correct. Any ideas?

推荐答案

通常你只需定义专业化 inline

Usually you'd just define the specializations inline in the header as dirkgently said.

您可以在单独的翻译单元中定义特殊化,但如果您担心编译时间或代码膨胀:

You can define specializations in seperate translation units though if you are worried about compilation times or code bloat:

// x.h:
template<class T> struct X {
    void f() {}
}

// declare specialization X<int>::f() to exist somewhere: 
template<> void X<int>::f();







// translation unit with definition for X<int>::f():
#include "x.h"
template<> void X<int>::f() {
    // ...
}


$ b b

是的,你的方法看起来不错。请注意,您只能使用完整专业化 来执行此操作,因此通常不切实际。

So yes, your approach looks fine. Note that you can only do this with full specializations, thus it is often impractical to do this.

有关详情, Comeaus模板常见问题

这篇关于模板类成员专门化的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆