简体   繁体   English

C ++:调用模板化类的非模板化函数

[英]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. 这意味着编译器将为每个具有不同TObject<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.

相关问题 模板成员函数不能从同一类C ++中调用非模板成员函数 - Templated member functions cannot call non-templated member function from same class, C++ 调用非模板化类c ++的模板化函数时出错 - Error calling templated function of non-templated class c++ 具有模板化和非模板化形式的C ++类 - C++ Class with templated and non-templated form C++ 隐式转换规则为什么以及如何区分模板转换函数和非模板转换函数? - Why and how do the C++ implicit conversion rules distinguish templated conversion functions from non-templated? C ++在调用时指定函数的非模板版本 - C++ specify non-templated version of function when calling 如何在非模板化的类中使用模板特化? C ++ - How can i use template specialization within a non-templated class? c++ 可以在C ++标头中实现类模板的非模板嵌套类吗? - Can non-templated nested classes of class templates be implemented in a C++ header? C++ 对待实例化模板 class 与非模板类型有什么不同吗? - Does C++ treat an instantiated template class any differently from a non-templated type? 如何专门化非模板类的模板成员函数? - how to specialize templated member functions of non-templated classes? 从模板化类定义非模板化类 - defining a non-templated class from a templated class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM