首页 > 解决方案 > 是否适合为每个“类型”创建一个带有构造函数重载的模板类

问题描述

我是使用模板的新手。

为各种实际类型使用构造函数重载创建模板类是否合适?

例如:

template <typename T>
class A
{
    A(..will receive paramaters for a specific type T);
    A(..will receive DIFFERENT params for a different type T);
}

代码将编译,因为每个实例化的所有功能都有效,但是从逻辑上(实际上)讲,每个构造函数都将根据我实际使用的类型被调用。

这很符合我的目的,但这是正确的“编程心态”吗?


更具体地说 - 使用 SFML,这就是我所指的,一个是精灵,另一个是形状,但具有通用功能,但也具有专有功能,例如 setColor 或 setTexture 函数。我的意思是,将 T 的第二个构造函数作为精灵调用是没有逻辑意义的:

template <typename T>
class GameObject {
public:
    GameObject(sf::Texture& texture, sf::Vector2f pos)
    {       

        m_drawable.setTexture(texture);
        m_drawable.setOrigin(m_drawable.getGlobalBounds().width / 2, m_drawable.getGlobalBounds().height / 2);
        m_drawable.setPosition(pos);
    }
    GameObject(sf::Color color, sf::Vector2f pos)
    {
        m_drawable.setColor(color);
        m_drawable.setPosition(pos);
    }




private:
    T m_drawable;

};

标签: c++templates

解决方案


据我了解,您只能使用GameObject<Sprite>GameObject<Shape>使用模板来分解一些通用代码,但是这两个类都有特定的接口。

然后,IMO,在接口方法中提供不使用是不好的。

std::vector提供了一些对类型有预先要求的方法Tv.resize(count)需要默认可插入)。但它是通用类,组合会很大,所以可以接受 IMO。

我认为不常用的方法应该从GameObject<T>

  • 在 C++20 中,requires允许有条件地“删除”方法:

    template <typename T>
    class GameObject {
    public:
        GameObject(sf::Texture&, sf::Vector2f pos) requires(std::is_same_v<Sprite, T>);
        GameObject(sf::Color, sf::Vector2f pos) requires(std::is_same_v<Shape, T>);
        // ...
    };
    

    SFINAE 是以前标准的替代品。

  • 您可能有 2 个新类GameSprite/GameShape使用/继承一个已清理GameObject<T>的作为实现细节。

  • 或者,您可以将 ( friend) 自由函数用于不同的行为

    template <typename T>
    class GameObject {
        GameObject(T&& t) : m_drawable(std::move(t)){}
    public:
        friend GameObject<Sprite> GameSprite(sf::Texture& texture, sf::Vector2f pos) {
            Sprite sprite;
    
            sprite.setTexture(texture);
            sprite.setOrigin(sprite.getGlobalBounds().width / 2, sprite.getGlobalBounds().height / 2);
            sprite.setPosition(pos);
            return GameObject<Sprite>{std::move(sprite)};
        }
        friend GameObject<Shape> GameShape(sf::Color color, sf::Vector2f pos) { /*..*/}
        // ...
    };
    

推荐阅读