专业的编程技术博客社区

网站首页 > 博客文章 正文

C++核心准则T.5:结合使用泛型和面向对象技术应该增强效果

baijin 2024-10-12 02:10:41 博客文章 13 ℃ 0 评论

T.5: Combine generic and OO techniques to amplify their strengths, not their costs

T.5:结合使用泛型和面向对象技术应该增强它们的效果而不是成本

Reason(原因)

Generic and OO techniques are complementary.

泛型和面向对象技术是互补的。

Example(示例)

Static helps dynamic: Use static polymorphism to implement dynamically polymorphic interfaces.

静态协助动态:使用静态多态技术实现动态多态接口。

class Command {
    // pure virtual functions
};

// implementations
template</*...*/>
class ConcreteCommand : public Command {
    // implement virtuals
};

Example(示例)

Dynamic helps static: Offer a generic, comfortable, statically bound interface, but internally dispatch dynamically, so you offer a uniform object layout. Examples include type erasure as with std::shared_ptr's deleter (but don't overuse type erasure).

动态帮助静态:提供通用,舒适的静态边界的接口,但是内部进行动态分发,这样就可以提供一致的对象布局。示例代码引入了和std::shared_ptr的删除器一样的类型消除机制。

#include <memory>

class Object {
public:
    template<typename T>
    Object(T&& obj)
        : concept_(std::make_shared<ConcreteCommand<T>>(std::forward<T>(obj))) {}

    int get_id() const { return concept_->get_id(); }

private:
    struct Command {
        virtual ~Command() {}
        virtual int get_id() const = 0;
    };

    template<typename T>
    struct ConcreteCommand final : Command {
        ConcreteCommand(T&& obj) noexcept : object_(std::forward<T>(obj)) {}
        int get_id() const final { return object_.get_id(); }

    private:
        T object_;
    };

    std::shared_ptr<Command> concept_;
};

class Bar {
public:
    int get_id() const { return 1; }
};

struct Foo {
public:
    int get_id() const { return 2; }
};

Object o(Bar{});
Object o2(Foo{});

Note(注意)

In a class template, non-virtual functions are only instantiated if they're used -- but virtual functions are instantiated every time. This can bloat code size, and may overconstrain a generic type by instantiating functionality that is never needed. Avoid this, even though the standard-library facets made this mistake.

在类模板中,非虚函数只有在被使用时才会实例化-但是虚函数任何时候都会实例化。这会使代码膨胀,并且因为实例化根本不用的功能而过度约束通用类型。要避免这个问题,即使标准库有时也会犯这样的错误。


Enforcement(实施建议)

See the reference to more specific rules.

参见更加具体的规则。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t5-combine-generic-and-oo-techniques-to-amplify-their-strengths-not-their-costs

新书介绍

《实战Python设计模式》是作者最近出版的新书,拜托多多关注!

本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。

对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。


觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表