与AI合作 -- 写一个modern c++单例工厂

这篇具有很好参考价值的文章主要介绍了与AI合作 -- 写一个modern c++单例工厂。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

前言

提问

bard给出的答案

AI答案的问题

要求bard改进

 人类智能

AI VS 人类


前言

通过本文读者可以学到modern C++单例模式+工厂模式的混合体,同时也能看到:如今AI发展到了怎样的智能程度?怎样让AI帮助我们快速完成实现头脑中的想法?以及这样的智能程度会不会让程序员失业?

提问

这次我不用ChatGPT了,而是换成了bard.

mzhai: Write a Modern c++ program to implement a singleton factory to produce 2 products. Each product has different arguments, the 1st has only one parameter of type int, the 2nd product has two parameters int and string respectively.

 这里我特意提出要创建两个参数不同的产品,防止AI写出来的类不通用(有点像避免算法过拟合的感觉)。

bard给出的答案

  1 #include <iostream>
  2 #include <memory>
  3 #include <utility>
  4
  5 enum class ProductType {
  6     Product1,
  7     Product2
  8 };
  9
 10 class Product {
 11 public:
 12     virtual ~Product() = default;
 13     virtual void use() = 0;
 14 };
 15
 16 // Concrete products with different arguments
 17 class Product1 : public Product {
 18 public:
 19     Product1(int arg1) : arg1_(arg1) {}
 20     void use() override { std::cout << "Using Product1 with arg1: " << arg1_ << std::endl; }
 21 private:
 22     int arg1_;
 23 };
 24
 25 class Product2 : public Product {
 26 public:
 27     Product2(int arg2_1, std::string arg2_2) : arg2_1_(arg2_1), arg2_2_(arg2_2) {}
 28     void use() override { std::cout << "Using Product2 with arg2_1: " << arg2_1_ << ", arg2_2: " << arg2_2_ << std::endl;     }
 29 private:
 30     int arg2_1_;
 31     std::string arg2_2_;
 32 };
 33
 34 // Singleton factory with modern techniques
 35 class ProductFactory {
 36 public:
 37     static ProductFactory& getInstance() {
 38         static ProductFactory instance;
 39         return instance;
 40     }
 41
 42     template <typename... Args>
 43     std::unique_ptr<Product> createProduct(ProductType type, Args&&... args) {
 44         switch (type) {
 45             case ProductType::Product1:
 46                 return std::make_unique<Product1>(std::forward<Args>(args)...);
 47             case ProductType::Product2:
 48                 return std::make_unique<Product2>(std::forward<Args>(args)...);
 49             default:
 50                 throw std::invalid_argument("Invalid product type");
 51         }
 52     }
 53
 54 private:
 55     ProductFactory() = default; // Private constructor for singleton
 56 };
 57
 58 int main() {
 59     ProductFactory& factory = ProductFactory::getInstance();
 60
 61     std::unique_ptr<Product> product1 = factory.createProduct(ProductType::Product1, 42);
 62     std::unique_ptr<Product> product2 = factory.createProduct(ProductType::Product2, 5, "Hello");
 63
 64     product1->use();
 65     product2->use();
 66
 67     return 0;
 68 }

AI答案的问题

第一次给出的答案编译失败,报错信息如下

/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/unique_ptr.h:1065:34: error: no matching constructor for initialization of 'Product2'
 1065 |     { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
      |                                  ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/insights/insights.cpp:48:29: note: in instantiation of function template specialization 'std::make_unique<Product2, int>' requested here
   48 |                 return std::make_unique<Product2>(std::forward<Args>(args)...);
      |                             ^

与AI合作 -- 写一个modern c++单例工厂,modern C++,c++,bard,AI,设计模式,design pattern有点奇怪吧,62行我们明明给Product2传的是两个参数,不是有Product2才走case ProductType::Product2吗(48行)?但编译器却抱怨给Product2的构造函数只传了一个int。这是为什么哪?

与AI合作 -- 写一个modern c++单例工厂,modern C++,c++,bard,AI,设计模式,design pattern这是因为case是运行时代码,而createProduct根据61、62行会产生两个重载,每个都包含全部代码。比如61行会产生如下code:

std::unique_ptr<Product> createProduct(ProductType type, int arg) {
    switch (type) {
        case ProductType::Product1:
            return std::make_unique<Product1>(arg);
        case ProductType::Product2:
            return std::make_unique<Product2>(arg);
        default:
            throw std::invalid_argument("Invalid product type");
    }
}

因为Product2的构造函数有两个参数,而此处只有一个(int arg), 故找不到匹配的构造函数。

要求bard改进

直接把错误贴给他,它还是没能正真理解编译失败的原因,之所以这么说,是因为它给出的解决方案是:

case ProductType::Product2:
                if constexpr (sizeof...(Args) == 2) { // Ensure 2 arguments for Product2
                    return std::make_unique<Product2>(std::forward<Args>(args)...);
                } else {
                    throw std::invalid_argument("Invalid arguments for Product2");
                }

 constexpr (sizeof...(Args) == 2) 确实在编译阶段就判断除了TRUE/FALSE:如果条件不符合,这段代码就不走了,从而导致61行实例出来的createProduct函数不报错,但62行依然会报错(看后面的cppinsights截图就会明白了)。我们暂且按照这种思路试一试, 虽然它有问题(后面会说)。

  1 #include <iostream>
  2 #include <memory>
  3 #include <utility>
  4
  5 class Product {
  6 public:
  7     virtual ~Product() = default;
  8     virtual void use() = 0;
  9 };
 10
 11 // Concrete products with different arguments
 12 class Product1 : public Product {
 13 public:
 14     Product1(int arg1) : arg1_(arg1) {}
 15     void use() override { std::cout << "Using Product1 with arg1: " << arg1_ << std::endl; }
 16 private:
 17     int arg1_;
 18 };
 19
 20 class Product2 : public Product {
 21 public:
 22     Product2(int arg2_1, std::string arg2_2) : arg2_1_(arg2_1), arg2_2_(arg2_2) {}
 23     void use() override { std::cout << "Using Product2 with arg2_1: " << arg2_1_ << ", arg2_2: " << arg2_2_ << std::endl;     }
 24 private:
 25     int arg2_1_;
 26     std::string arg2_2_;
 27 };
 28
 29 class ProductFactory {
 30 public:
 31     static ProductFactory& getInstance() {
 32         static ProductFactory instance;
 33         return instance;
 34     }
 35     template <typename... Args>
 36     std::unique_ptr<Product> createProduct(Args&&... args) {
 37         if constexpr (sizeof...(Args) == 1){
 38             return std::make_unique<Product1>(std::forward<Args>(args)...);
 39         }
 40         if constexpr (sizeof...(Args) == 2){
 41             return std::make_unique<Product2>(std::forward<Args>(args)...);
 42         }
 43     }
 44 private:
 45     ProductFactory() = default; // Private constructor for singleton
 46 };
 47
 48 int main() {
 49     ProductFactory& factory = ProductFactory::getInstance();
 50
 51     std::unique_ptr<Product> product1 = factory.createProduct(42);
 52     std::string hi("Hello");
 53     std::unique_ptr<Product> product2 = factory.createProduct(5, hi);
 54
 55     product1->use();
 56     product2->use();
 57
 58     return 0;
 59 }

编译成功,运行也没问题。我们还可以通过cppinsights.io看看模板函数实例化出来的2个重载结果:

与AI合作 -- 写一个modern c++单例工厂,modern C++,c++,bard,AI,设计模式,design pattern

 显然这种办法不好:

  1. 请想一想如果product1 和Product2都只有一个参数哪?就无法通过sizeof区分了。
  2. 而且,这么做产生了很多无用代码(if constexpr...)

人类智能

所以还是由我给它快速改一改吧。我们直接给createProduct函数多传一个模板参数进去,比如叫T,像下面这样:

template <typename T, typename... Args>
std::unique_ptr<T> createProduct(Args&&... args) {
    return std::make_unique<T>(std::forward<Args>(args)...);
}

...

std::unique_ptr<Product> product1 = factory.createProduct<Product1>(42);
std::string hi("Hello");
std::unique_ptr<Product> product2 = factory.createProduct<Product2>(5, hi);

看下createProduct如何实例化的?

与AI合作 -- 写一个modern c++单例工厂,modern C++,c++,bard,AI,设计模式,design pattern

 问一下bard这么改怎么样?回答的井井有条!

mzhai:How about changing the code to above?

Bard: I'm happy to review the proposed code changes:

Key Changes:

  • Templated createProduct function: The createProduct function now takes a template parameter T to specify the product type, eliminating the need for the ProductType enum. (传了T就没必要传ProductType enum了)
  • Removal of switch statement: The switch statement is no longer needed as the compiler determines the correct type based on the template argument.(createProduct中的Switch可以去掉了)
  • Explicit product type in main: The createProduct function is called with explicit template arguments (Product1 and Product2) to create the desired product types.(main中要显示指定产品类型,即尖括号中的Product1、Product2)

Analysis:

  • Pros:
    • More concise and less verbose code. (代码简明)
    • Leverages template metaprogramming for type-driven behavior.(利用了模板元编程)
    • Potentially easier to extend for new product types.(新产品类型扩展更容易)
  • Cons:
    • Might be less readable for those less familiar with templates.(对不熟悉模板的人来说可读性变差)
    • Error messages might be more cryptic due to template instantiation.(因为模板实例导致报错信息比较神秘不那么直接,确实!)
    • Doesn't enforce a closed set of product types (could potentially create other types).(没有强制指定哪些产品类型能生产,有产生别的类型的风险)

Overall:

  • The changes are valid and adhere to modern C++ practices.
  • The choice between this approach and the previous one using the ProductType enum is mainly a matter of preference and trade-offs.
  • Consider the team's familiarity with templates and the specific project requirements when deciding on the best approach.

Additional Notes:

  • Ensure that all product classes (Product1Product2, etc.) are derived from the Product base class for this approach to work correctly.
  • Consider using static_assert or concepts (if available in your C++ version) to enforce constraints on the template parameter T, ensuring it's a valid product type.

AI VS 人类

AI确实智商还有限,但也确实能提高编程效率,copilot等等一般宣传能提高人类编程效率50%左右,在没有增量就业机会的情况下也就是一半工程师要失业了?? 

附上最终代码

便于大家COPY及实验。文章来源地址https://www.toymoban.com/news/detail-833142.html

#include <iostream>
#include <memory>
#include <utility>

class Product {
public:
    virtual ~Product() = default;
    virtual void use() = 0;
};

// Concrete products with different arguments
class Product1 : public Product {
public:
    Product1(int arg1) : arg1_(arg1) {}
    void use() override { std::cout << "Using Product1 with arg1: " << arg1_ << std::endl; }
private:
    int arg1_;
};

class Product2 : public Product {
public:
    Product2(int arg2_1, std::string arg2_2) : arg2_1_(arg2_1), arg2_2_(arg2_2) {}
    void use() override { std::cout << "Using Product2 with arg2_1: " << arg2_1_ << ", arg2_2: " << arg2_2_ << std::endl; }
private:
    int arg2_1_;
    std::string arg2_2_;
};

class ProductFactory {
public:
    static ProductFactory& getInstance() {
        static ProductFactory instance;
        return instance;
    }

    template <typename T, typename... Args>
    //typename std::enable_if<std::is_same<Product,T>::value, void>::type
    std::unique_ptr<T> createProduct(Args&&... args) {
        return std::make_unique<T>(std::forward<Args>(args)...);
    }

private:
    ProductFactory() = default; // Private constructor for singleton
};

int main() {
    ProductFactory factory;

    std::unique_ptr<Product> product1 = factory.createProduct<Product1>(42);
    std::string hi("Hello");
    std::unique_ptr<Product> product2 = factory.createProduct<Product2>(5, hi);

    product1->use();
    product2->use();

    return 0;
}

到了这里,关于与AI合作 -- 写一个modern c++单例工厂的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Java设计模式---单例 工厂 代理模式

    单例模式是设计模式中的一种,属于创建型模式。在软件工程中,单例模式确保一个类只有一个实例,并提供一个全局访问点。这种模式常用于那些需要频繁实例化然后引用,且创建新实例的开销较大的类,例如数据库连接池、缓存管理等。 意图 :保证一个类仅有一个实例

    2024年01月24日
    浏览(39)
  • 设计模式(单例模式,工厂模式),线程池

    目录 什么是设计模式? 单例模式 饿汉模式 懒汉模式 工厂模式 线程池 线程池种类 ThreadPoolExcutor的构造方法: 手动实现一个线程池  计算机行业程序员水平层次不齐,为了 让所有人都能够写出规范的代码, 于是就有了设计模式, 针对一些典型的场景,给出一些典型的解决方案 单例

    2024年02月11日
    浏览(30)
  • 设计模式-创建型模式(单例、工厂、建造、原型)

    设计模式:软件设计中普遍存在(反复出现)的各种问题,所提出的解决方案。 面向对象三大特性:封装、继承、多态。 面向对象设计的SOLID原则: (1)开放封闭原则:一个软件实体如类、模块和函数应该对扩展开放,对修改关闭。即软件实体应尽量在不修改原有代码的情

    2024年02月08日
    浏览(43)
  • Python入门【​编辑、组合、设计模式_工厂模式实现 、设计模式_单例模式实现、工厂和单例模式结合、异常是什么?异常的解决思路 】(十七)

    👏作者简介:大家好,我是爱敲代码的小王,CSDN博客博主,Python小白 📕系列专栏:python入门到实战、Python爬虫开发、Python办公自动化、Python数据分析、Python前后端开发 📧如果文章知识点有错误的地方,请指正!和大家一起学习,一起进步👀 🔥如果感觉博主的文章还不错的

    2024年02月14日
    浏览(28)
  • java基础之设计模式(单例模式,工厂模式)

    是一种编码套路 单例模式 一个类只能创建一个实例 饿汉式 直接创建唯一实例 缺点: 有可能浪费空间 懒汉式 在获取实例是创建唯一对象 缺点: 线程效率慢 懒汉式-进阶版 在懒汉式的基础上,利用同步代码块结合二次校验提高执行效率 工厂模式 是一种底层技术,通常用于底层框

    2024年01月18日
    浏览(33)
  • 【java设计模式】创建型模式介绍(工厂模式、抽象工厂模式、单例模式、建造者模式、原型模式)

    简介 本文介绍Java设计模式中创建型模式的五种 一、工厂模式 工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。 工厂模式提供了一种将对象的实例化过程封装在工厂类中的方式。通过使用工

    2024年02月16日
    浏览(39)
  • day39 注解 设计模式(单例模式和工厂模式)

    @Target    @Target(ElementType.TYPE) @Target({                 ElementType.TYPE,                          ElementType.METHOD}) //确定自定义的注解的使用范围 type为类  method为方法  field为属性 @Retention @Retention()  //(不添加参数默认是class) @Retention(RetentionPolicy.CLASS)// 保留到

    2024年02月09日
    浏览(27)
  • Java中23种设计模式-单例模式--工厂模式

    加油,新时代打工人! 23种设计模式定义介绍 Java中23种设计模式-单例模式 Java中23种设计模式-单例模式2–懒汉式线程不安全 Java中23种设计模式-单例模式2–懒汉式2线程安全 Java中23种设计模式-单例模式–饿汉式 定义: 工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一

    2024年02月21日
    浏览(33)
  • 前端设计模式:工厂方法模式、单例模式、订阅模式、中介者模式

    工厂方法模式是一种创建型设计模式,它提供了一种将对象的创建与使用分离的方式。在工厂方法模式中,我们定义一个工厂接口,该接口声明了一个用于创建对象的方法。具体的对象创建则由实现该接口的具体工厂类来完成。 工厂方法模式的核心思想是将对象的创建延迟到

    2024年02月12日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包