gpt4 book ai didi

c++ - 返回没有类型参数的 std::make_unique

转载 作者:行者123 更新时间:2023-11-27 23:40:15 24 4
gpt4 key购买 nike

<分区>

例如我得到了这段代码:

#include <memory>

class A {
public:
A(int i, float f){};
A(std::string){};
...
};

class B {
public:
B(int i){};
B(float f){};
...
};

std::unique_ptr<A> getA() {
if (some condition) {
return std::make_unique<A>(1,1.0);
} else if (some condition) {
return std::make_unique<A>("test");
}
}

std::unique_ptr<B> getB() {
if (some condition) {
return std::make_unique<B>(1);
} else if (some condition) {
return std::make_unique<B>(1.0);
}
}

我可以像这样写代码吗return std::make_unique(1,1.0); (没有 <A> )?我认为编译器知道返回类型,并且只有类 A 的构造函数参数在这一行中很重要。那么有什么办法可以省略类型参数吗?

我想要这个是因为在实际问题中,有很多类类型(名称很长),每个类都有很多构造函数,写起来很烦人 return make::unique<*type*>(*parameters*)每次回来的时候。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com