简体   繁体   English

最好不要对模板化类分开函数声明和定义吗?

[英]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.

相关问题 为什么将 function 声明和定义放在单独的文件中? - Why put function declarations and definitions in separate files? 自动将 class 定义与声明分开? - Automatically separate class definitions from declarations? 使用C ++模块时,是否有任何理由将函数声明(.hpp文件)与其定义(.cpp文件)分开? - When using C++ modules, is the any reason to separate function declarations (.hpp files) from their definitions (.cpp files)? 根据函数定义进行函数声明 - Make function declarations based on function definitions C ++模板化函数和前向声明 - C++ templated function and forward declarations 程序顶部的声明与定义(最佳实践) - Declarations vs. Definitions at the top of your program (Best Practice) 将代码分成块是一种好习惯吗? - Is it good practice to separate code into blocks? 解析模板类的专用静态成员变量的定义 - Resolving Definitions of Specialized Static Member Variables of Templated Classes typedeffing模板化基类是简化代码的一个好习惯吗? - Is typedeffing templated base class to simplify the code a good practice? 如何区分 Clang AST 访问者中的函数定义和函数声明 - How to distinguish function definitions and function declarations in Clang AST visitors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM