[英]C++: call non-templated functions of a templated class
Is it somehow possible to have callable, non-templated functions in a templated class? 是否可以在模板化类中具有可调用的非模板化函数? Like so: 像这样:
template<typename T>
class Object
{
public:
static int loadInfo() { return 0;}
// more code
};
I'd like to write, eg: 我想写,例如:
Object::loadInfo();
instead of 代替
Object<int8_t>::loadInfo();
It's not virtually important, would just be some 'syntactic sugar'. 它实际上并不重要,而只是一些“语法糖”。 A bit more background: the function I am thinking of would load some data from disk upon which the program decides which templated version of the object it has to use. 背景知识:我正在考虑的功能将从磁盘上加载一些数据,程序在该数据上决定必须使用的对象的模板版本。 ATM it looks like this: ATM看起来像这样:
auto usewhat=Object<char>::loadInfo(); // this line feels so wrong
if(usewhat == 0) {
Object<int8_t> o;
o.doSomething();
}else if(usewhat == 1){
Object<int32_t> o;
o.doSomething();
}else{
Object<int64_t> o;
o.doSomething();
}
The only possibilities I can think of atm are 1) using a non-template base class of a different name (say, "BaseObject") or 2) using another class with a different name (say, "InfoObject") ... but both possibilities do not seem too appealing. 我能想到的atm的唯一可能性是1)使用不同名称的非模板基类(例如,“ BaseObject”)或2)使用其他名称不同的类(例如,“ InfoObject”)...但是两种可能性似乎都不太吸引人。
You should make loadInfo
a free function (maybe with a different name like loadObjectInfo
or whatever makes sense to indicate the semantic relationship with Object
). 您应该使loadInfo
成为一个自由函数(可以使用不同的名称(例如loadObjectInfo
或任何有意义的方式来指示与Object
的语义关系)。
Even though it is a static
function and does not depend on a particular instance of Object
, it still depends on Object
's template parameters: The function may use T
, even though in your case it obviously doesn't. 即使它是static
函数并且不依赖于Object
的特定实例,它仍然依赖于Object
的模板参数:该函数可以使用T
,即使在您的情况下显然不使用T
This means that the compiler will generate new code for each Object<T>::loadInfo
with different T
's, meaning slower builds and potentially larger binaries. 这意味着编译器将为每个具有不同T
的Object<T>::loadInfo
生成新代码,这意味着生成速度较慢,并且二进制文件可能更大。 Also, you cannot implement the function in a cpp file. 同样,您不能在cpp文件中实现该功能。
If you have a copy of Scott Meyers' Effective C++ (3rd Edition) available, you might want to look up Item 44 ("Factor parameter-independent code out of templates"). 如果您有可用的Scott Meyers的Effective C ++(第3版)的副本,则可能要查找项目44(“模板中与因子参数无关的代码”)。
Not directly, but you can use a typedef
: 不直接,但您可以使用typedef
:
typdef Object<int8_t> MyObject;
MyObject::loadInfo();
If the function can be moved out of the class, do it. 如果该函数可以移出该类,请执行此操作。
If it cannot be moved out of the class, but does not depend on T, your class is too fat and should be split. 如果不能将其移出班级,但不依赖于T,则您的班级太胖了,应该拆分。
class ObjectBase {
// stuff that doesn't depend on T
static int loadInfo();
};
template <typename T>
class Object : public ObjectBase {
// more stuff
};
Actually I suppose there is no portable way of calling functions of template class without specifying template. 实际上,我认为没有指定模板就没有可移植的方式来调用模板类的函数。 I also suggest keeping your code portable as much as possible because it will likely help you in the future (with other compiler of with another version of compiler you currently use). 我还建议尽可能保持代码的可移植性,因为这可能会在将来对您有所帮助(使用其他编译器以及您当前使用的其他版本的编译器)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.