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

时间:2022-07-24
本文章向大家介绍C++核心准则T.5:结合使用泛型和面向对象技术应该增强它们的效果而不是成本,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

天人菊

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