在哪里/如何定义模板

发布于 2024-08-21 00:01:24 字数 367 浏览 5 评论 0原文

在 C++ 中定义模板的最佳实践是什么?

template <class T>
class A
{
private:
    // stuff
public:
    T DoMagic()
    {
        //method body
    }
}

Or:

template <class T>
class A
{
private:
    // stuff
public:
    T DoMagic();
}

template <class T>
A::T DoMagic()
{
    // magic
}

另一种方式? 我似乎无意中发现了有关这个主题的一些争议。 所以;选择什么道路?

What is the best pratice in regards to defining a template in C++?

template <class T>
class A
{
private:
    // stuff
public:
    T DoMagic()
    {
        //method body
    }
}

Or:

template <class T>
class A
{
private:
    // stuff
public:
    T DoMagic();
}

template <class T>
A::T DoMagic()
{
    // magic
}

Another way?
I seem to stumble over some controversy regarding this subject.
So; What path to choose?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浮华 2024-08-28 00:01:24

这完全是风格问题。不过,话虽如此:

  1. 选择一种方式并坚持下去——要么全部内联,要么全部外联,或者根据某些规则进行混合,
  2. 我个人使用 3 行规则。如果模板中的方法主体超过 3 行,我会将其移到外面。

没有真正的理由不包含所有内联定义(无论如何,它们都是从编译器 POV 内联的),但是,许多人认为将它们分开会更干净,并且允许类定义更具可读性。

This is completely a matter of style. That said however:

  1. choose a way and stick to it -- either all inline, or all out, or mixed based on some rule
  2. personally I use a 3 line rule. If the method body in the template is longer than 3 lines I move it outside.

There's no real reason not to include all definitions inline (they are inline from the compilers POV anyway), however, many people argue that keeping them separate is more clean, and allows the class definition to be more readable.

揪着可爱 2024-08-28 00:01:24

随着您编写的模板变得越来越大、越来越复杂,请使用越来越高的分离级别。
无论如何将定义与声明分开,性能都是相同的,因此这里主要关注的应该是可读性和可维护性。

当编写仅在一个地方使用的简单模板时,请根据要使用它的 CPP 文件中的声明内联声明和定义它。如果只有一个代码块需要此模板,则没有理由强制进行全局重新编译。

file.cpp

template<class Gizmo> bool DoSomethingFancy()
{
 // ...
}

对于跨翻译单元使用的小型模板实用程序,请在 H 文件中一起定义和声明它们:

utility.h

template<class Gizmo> bool DoSomethingUseful() 
{
  // ...
}

随着模板变得越来越复杂,能够从定义中单独查看声明将变得更加重要。首先,将所有内容分开,但放在同一个文件中:

utility.h

template<class Type> class Useful
{
  bool FunctionA();
  bool FunctionB();
};

template<class Type> bool Useful<Type>::FunctionA()
{
  // ...
}

template<class Type> bool Useful<Type>::FunctionB()
{
  // ...
}

但最终,即使这样也会变得笨拙。当它出现时,将其分成一个用于声明的头文件和一个用于定义的 INC 文件。在头文件末尾,#include INC 文件:

utility.h :

template<class Type> class MoreUseful
    {
      bool FunctionA();
      bool FunctionB();
    };

#include "utility.inc"

utility.inc :

template<class Type> bool MoreUseful<Type>::FunctionA()
{
  // ...
}

template<class Type> bool MoreUseful<Type>::FunctionB()
{
  // ...
}

Use an increasing level of separation as the templates you write grow larger and more complex.
Performance will be the same no matter how you separate the definitions from the declarations, so your main concern here should be readability and maintainability.

When writing a simple template used only in one place, declare and define it inline with the declarations right in the CPP file where you're going to use it. There's no reason to force a global recompile if only one block of code needs this template.

file.cpp

template<class Gizmo> bool DoSomethingFancy()
{
 // ...
}

For small template utilities used across translation units, define and declare them together in an H file:

utility.h

template<class Gizmo> bool DoSomethingUseful() 
{
  // ...
}

As your templates become more complex it will become more important to be able to view the declaration separately from the definition. At first, keep everything separate but in the same file:

utility.h

template<class Type> class Useful
{
  bool FunctionA();
  bool FunctionB();
};

template<class Type> bool Useful<Type>::FunctionA()
{
  // ...
}

template<class Type> bool Useful<Type>::FunctionB()
{
  // ...
}

But eventually even this will become unwieldly. When it does, separate it in to a header file for the declarations, and an INC file for the definitions. At the end of the header file, #include the INC file:

utility.h :

template<class Type> class MoreUseful
    {
      bool FunctionA();
      bool FunctionB();
    };

#include "utility.inc"

utility.inc :

template<class Type> bool MoreUseful<Type>::FunctionA()
{
  // ...
}

template<class Type> bool MoreUseful<Type>::FunctionB()
{
  // ...
}
往日情怀 2024-08-28 00:01:24

这是一个宗教(风格)问题。对于具有多个方法或少数方法很简单的类,我更喜欢在模板声明之外定义函数。

无论哪种情况,我的理解是模板声明和方法定义必须位于同一翻译单元中。这是因为模板更像是模板,编译器将给定类型插入模板并为给定类型生成代码。

无论你做出什么决定,只要保持一致即可。

This is a religious (style) issue. I prefer to define my functions outside of the template declaration for classes that have more than one method or the few methods are simple.

In either case, my understanding is that the template declaration and the method definitions must be in the same translation unit. This is because the template is more like a stencil, the compiler plugs a given type into the stencil and generates code for the given type.

Whatever you decide, just be consistent.

长安忆 2024-08-28 00:01:24

我通常在外部定义所有方法,但每次我希望 C++ 有某种“模板块”:

template <class T>
struct A
{
     T foo();
     T bar(T * t);
     T baz(T const & t);
};

template <class T>  // Made-up syntax
{
    T A::foo()
    {
        //...
    }

    T A::bar(T * t)
    {
        //...        
    }

    T A::baz(T const & t)
    {
        //...
    }
}

I usually define all the methods outside but each time I wish C++ had some sort of "template blocks":

template <class T>
struct A
{
     T foo();
     T bar(T * t);
     T baz(T const & t);
};

template <class T>  // Made-up syntax
{
    T A::foo()
    {
        //...
    }

    T A::bar(T * t)
    {
        //...        
    }

    T A::baz(T const & t)
    {
        //...
    }
}
阿楠 2024-08-28 00:01:24

如果函数很重要(即多于一行或两行),请考虑单独定义它们。这使得类的接口对于类的用户来说更容易导航、阅读和理解,他们很可能不必查看每个方法的实际实现。

If the functions are non-trivial (i.e. more than one or two lines), consider defining them separately. This makes the interface of the class much easier to navigate, read and understand for the users of your class, who most likely shouldn't have to look at the actual implementation of each method.

够钟 2024-08-28 00:01:24

对于像您的示例这样的一次性实例,这几乎没有什么区别。

当存在大量具有多种变化的模板时会发生什么?然后,它有助于将相似类型的苹果放在一起,并将相似类型的橙子放在远离它们的地方。当然,这一切都必须直观且实用地完成。这很大程度上受到程序员使用代码的文化的影响。

For a one-off instance like your example, it makes little difference.

What happens when there are lots of templates with lots of variations? It then helps to put similar types of apples together, and similar types of oranges together away from them. Of course, this must all be done as intuitively as practical. That is greatly affected by the culture of programmers working with the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文