[英]Is it a good practice to not to separate function declarations and definitions for templated classes?
Generally in non templated classes, we separate the function declarations and definitions into separate files (.h and .cpp) 通常,在非模板化类中,我们将函数声明和定义分为单独的文件(.h和.cpp)
[1] But the above practice does not seem to work very good with templated classes. [1]但是,以上做法似乎不适用于模板化类。 Is it advised to write implementations in a separate file and then include it at the bottom of .h file ? 是否建议在单独的文件中编写实现,然后将其包含在.h文件的底部?
[2] Which of the below scheme is generally advised for templated classes? [2]对于模板类,通常建议采用以下哪种方案?
[a] declarations and definition all at once or [a]同时声明或定义
[b]seperated declarations & definitions in the same file [b]同一文件中的分隔声明和定义
Given the complicated syntax that we have to take care of if we go by choice [b] 考虑到语法的复杂性,如果我们选择[b],则必须注意
Eg. 例如。 [a] [一种]
template <typename T>
class unique_ptr final {
private:
T* ptr_;
public:
unique_ptr (T* ptr = nullptr) noexcept {
: ptr_{ ptr } {
}
friend bool operator == (const unique_ptr& lhs, const unique_ptr& rhs) {
return lhs.get() == rhs.get();
}
};
[b] 并[b]
template <typename T>
class unique_ptr final {
private:
T* ptr_;
public:
unique_ptr (T* ptr = nullptr) noexcept;
friend bool operator == (const unique_ptr& lhs, const unique_ptr& rhs);
/*** implementations inside the class after all declarations (I am not sure if this makes the code code any easier to understand) ***/
};
/**** Implementations outside the class ***/
/*** Convoluted things needed to make friend functions work ***/
/** like mentioned in : https://stackoverflow.com/questions/3989678/c-template-friend-operator-overloading ***/
Some functions, like "Koenig operators", cannot be defined outside of the class itself: 某些函数(例如“ Koenig运算符”)不能在类本身之外定义:
friend bool operator == (const unique_ptr& lhs, const unique_ptr& rhs) {
return lhs.get() == rhs.get();
}
this is a non-template friend of unique_ptr<T>
, generated for each template instantiation of unique_ptr
. 这是一个非模板朋友unique_ptr<T>
对于每个模板实例生成unique_ptr
。 There is no syntax in C++ that permits defining its body outside of unique_ptr
. 在C ++中,没有语法允许在unique_ptr
之外定义其主体。 (You can create template friends which are defined outside, but not non-template friends whose arguments are dependent on the template arguments of the template class). (您可以创建在外部定义的模板朋友,但不能创建其参数取决于模板类的模板参数的非模板朋友)。
We can get around this by: 我们可以通过以下方法解决此问题:
friend bool operator == (const unique_ptr& lhs, const unique_ptr& rhs) {
return equal(lhs, rhs);
}
and then define equal
as a template friend of unique_ptr
. 然后将equal
定义为unique_ptr
的模板朋友。
But even there, you could do: 但是即使在那儿,您也可以这样做:
template <typename T>
class unique_ptr final {
private:
T* ptr_;
public:
unique_ptr (T* ptr = nullptr) noexcept {
: ptr_{ ptr } {
}
friend bool operator == (const unique_ptr& lhs, const unique_ptr& rhs)
#include "unique_ptr_t_operator_equal_function_body.inc"
};
if you really wanted to split implementation and interface. 如果您真的想拆分实现和接口。
There are no technical barriers to separating implementation and interface into separate .h
and .inc
files, placing definitions inline in the template declaration, or placing the definition at the end of the .h
file. 将实现和接口分为单独的.h
和.inc
文件,将定义内联放在模板声明中或将定义放在.h
文件的末尾没有技术障碍。 Using multiple files has a tiny impact on compile times (as the filesystem or a cache of same usually has to be touched on a #include
), but that isn't usually large compared to other factors. 使用多个文件对编译时间影响很小(因为通常必须在#include
上触摸文件系统或相同的缓存),但是与其他因素相比,通常不会很大。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.