有模板方法但不公开实现 [英] Have a template method but not expose implementation

查看:82
本文介绍了有模板方法但不公开实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在TFRuntime.h中有一个函数

I have a function in TFRuntime.h

class TFRuntime {
...
    template <typename T>
    Status computeXYSlice(Volume<T>* input, int zCoord, Volume<T> *output);
...
}

TFRuntime.cpp包含诸如tensorflow库头

TFRuntime.cpp includes tensorflow library headers such as

#include <tensorflow/cc/ops/standard_ops.h>
#include <tensorflow/cc/saved_model/loader.h>

我不想在标头中包含这些内容,因为这将迫使使用TFRuntime的任何人也将它们包含在内.但是,如果我希望computeXYSlice函数允许任何类型,则必须在.h文件中包括实现.然而,该实现需要上面提到的张量流头.

I do not want to make these includes in the header since that would force anyone using TFRuntime to include them as well. However if i want the computeXYSlice function to allow any type, I have to include the implementation in the .h file. The implementation however requires the above mentioned tensorflow headers.

如何解决这个问题?我可以明确地仅实例化" computeXYSlice函数的某些变体吗?例如,其中Tfloatintdouble?还是有更好的方法?

How do I get around this problem? Could I explicitly 'instantiate' only certain variants of the computeXYSlice function? E.g., where T is float or int or double? Or is there a better way?

推荐答案

我可以只显式地实例化" computeXYSlice函数的某些变体吗?例如,其中T为float或int或double?

Could I explicitly 'instantiate' only certain variants of the computeXYSlice function? E.g., where T is float or int or double?

您可以,并且它们的实现不必在标头中.一会儿,我会解决的.但是,如果您真的想允许任何类型,则您的模板必须位于标头中.就是这样.

You may, and their implementation need not be in the header. I'll get to that in a moment. But if you really want to allow any type then your template must be in a header. That's just how it is.

如果您希望仅支持一小部分类型(如模板实例化)而没有过载(有时会在查找时有所作为),则该标准具有显式模板实例化的机制.

If you wish to support only a small set of types, as template instantiations, without overloading (can make a difference sometimes when doing lookup), the standard has a mechanism for explicit template instantiation.

您的标题看起来像这样...

Your header will look something like this...

class TFRuntime {
public:
    template <typename T>
    Status computeXYSlice(Volume<T>* input, int zCoord, Volume<T> *output);
};

...而您的实现文件将包含显式实例化 definitions ,就像这样...

... And your implementation file, will contain the explicit instatiation definitions, like so...

template <typename T>
Status TFRuntime::computeXYSlice(Volume<T>* input, int zCoord, Volume<T> *output) {
  // Implement it
}

template
Status TFRuntime::computeXYSlice(Volume<int>*, int, Volume<int>*);

template
Status TFRuntime::computeXYSlice(Volume<double>*, int, Volume<double>*);

您必须包括显式的实例化定义,否则您的程序格式错误,不需要诊断. 除非隐式实例化发生,否则必须定义模板函数,除非显式实例化出现在某处.

You have to include the explicit instantiation definitions, otherwise your program is ill-formed, no diagnostic required. The template function must be defined when implicit instantiation occurs, unless an explicit instantiation appears somewhere.

这有点麻烦.但是,如果您的最终目标是确实有很多实例化(而不是重载),那么这就是将它们捆绑在一起的方式.

This is a bit cumbersome. But if your end goal is to indeed have a bunch of instantiations (as opposed to overloads), that's how you tie it all together.

这篇关于有模板方法但不公开实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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