C++模板编程结构与模板

发布于 2024-11-02 01:54:12 字数 257 浏览 5 评论 0原文

为了为类 cls 提供功能,例如“add”,我决定使用以下类型:

struct add
{
      add sum(add other) { ... }
};

vs.

template<typename Add>
Add sum(Add one, Add two) { ... }

应首选哪种方法,第一种方法相对于第二种方法有什么优势,反之亦然?

To provide a functionality for a class cls, say, 'add', following are the types I decided:

struct add
{
      add sum(add other) { ... }
};

vs.

template<typename Add>
Add sum(Add one, Add two) { ... }

Which approach should be preferred, what advantages do the first have over the second and vice versa?

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

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

发布评论

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

评论(4

你的背包 2024-11-09 01:54:12

应该首选哪种方法,第一种方法相对于第二种方法有什么优势,反之亦然?

我相信这个问题没有一个简单的答案。我认为这可以归结为通用与面向对象编程之战。

另请参阅关于 C++ 中面向对象编程和泛型编程之间的紧张关系

Which approach should be preferred, what advantages do the first have over the second and vice versa?

I believe the question does not have a straightforward answer. I think this goes down to the Generic Vs Object Oriented Programming battle.

See also On the Tension Between Object-Oriented and Generic Programming in C++.

你丑哭了我 2024-11-09 01:54:12

你的例子之间有两点不同。首先,正如您正确指出的那样,您的第二个示例是使用函数模板。但其次,它使用的是自由函数,而不是成员函数。

为了公平比较,您应该比较:

add sum(add one, add two) { ... }

和:

template<typename Add>
Add sum(Add one, Add two) { ... }

虽然第二种变体可能更有用,但它取决于您将如何编写函数体。它可以用通用的方式编写,并且仍然对 add 对象执行操作吗?

There are two things different between your examples. Firstly, as you rightly point out, your second example is using a function template. But secondly, it's using a free function, vs. a member-function.

For a fair comparison, you ought to be comparing:

add sum(add one, add two) { ... }

and:

template<typename Add>
Add sum(Add one, Add two) { ... }

Whilst the second variation could be more useful, it depends on how you're going to write the function body. Can it be written in a generic way, and still perform operations on add objects?

嘿嘿嘿 2024-11-09 01:54:12

第一个对我来说没有意义,或者它太糟糕了以至于它不应该有意义,因为你编写一个类只是为了执行一些“操作”。一个类应该代表面向对象编程中的一个对象(或者如果它是类模板,它可以充当模板元编程中的元函数)。

对于操作来说,函数对对象进行操作是有意义的。所以第二个是有道理的,但我会将类型参数的 name 更改为:

template<typename T>
T sum(const T & one, const T & two) { ... }

毕竟,Add 是什么?

First one doesn't make sense to me, or it is so bad that it shouldn't make sense, because you write a class just to perform some "operation". A class is supposed to represent an object in object-oriented programming (Or if its class template, it can act as meta-function in template meta-programmig).

For operation, functions make sense that operate on object(s). So the second one makes sense, but I would change the name of type argument as:

template<typename T>
T sum(const T & one, const T & two) { ... }

After all, what was Add?

深海夜未眠 2024-11-09 01:54:12

何时做出依赖于类型的决策?如果运行时那么你必须使用虚方法,如果编译时那么你可能可以使用模板代替(当然,即使类型在运行时固定,你也可以使用虚方法,但这效率较低)。

如果类型在运行时是固定的,您可以使用模板一些技巧来避免告诉您正在使用哪些类型,并让编译器找到它们并生成正确的代码。

但请注意,C++ 不允许对模板函数进行部分特化,只允许对模板类进行部分特化,因此为了获得最大的灵活性,您可能需要将功能包装为模板结构的方法(即使是静态方法也可以)。一旦您有了模板类编译时调度机制,您就可以添加模板函数以允许类型推导并简化语法。

When are the type-dependent decisions going to be made? if runtime then you must use virtual methods, if compile time then you can probably use templates instead (of course you can use virtual methods even if the type is fixed at runtime, but this is less efficient).

If the types are fixed at runtime you can use template some tricks to avoid telling which types you're using and having the compiler finding them and generate proper code.

Note however that C++ doesn't allow partial specialization on template functions, only on template classes so for maximum flexibility you may need to wrap your functionalities as methods of template structures (even static methods are ok). Once you have your template class compile-time dispatching machinery in place you can add template functions to allow type deduction and simplify the syntax.

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