C++等效于C#9.0 New()与推论类型
C#9+知道从LHS推断RHS的类型,只需使用正确的参数调用new
并具有您的对象。无需重复冗长的类型,类型总是正确的,
List<int> ints = New(capacity: 10_000);
可以在C ++中完成相同的干新
吗?
我使用“安全”的新和删除有类似的东西,这些新和删除将目标作为参数和使用模板推断出类型等,但是它比my_var = new(arg1,arg1,arg2,arg2)>
请注意,这是一个有关如何做a = new(1,2,3)
(例如C#)的问题; vector&lt; toupel&lt; int,float,car&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&lt; vector&lt; vector&lt; toupel&lt; int,float,float,char&gt;&gt;&gt;&gt;&gt;&gt;》 '有时用shared_ptr
和CO包装。
C# 9+ knows to deduce the type of RHS from LHS, just call New
with the correct parameters and have your object. No need to repeat lengthy types, and the type is always correct
List<int> ints = New(capacity: 10_000);
Can the same DRY new
be done in c++ ?
I have something similar using "safe" new and delete that take the target as argument and deduce type etc using templates, but it is a bit less readable than just my_var = new(arg1, arg2, arg3)
Please note this is a question about how to do a = new(1,2,3)
, like C#, and not about now to create new objects in c++ which make look something like list<vector<toupel<int,float,char>>>*a = new list<vector<toupel<int,float,char>>>()
' sometimes wrapper with shared_ptr
and co.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,您应该很少在现代C ++中使用明确的内存管理。使用智能指针,例如
std :: simelor_ptr
带有所有权语义。
至于您的问题,您可以以大致相同的方式执行此操作,但可以相反:声明中的类型自动划分,并指定
new
表达式中的类型:或使用唯一指针:
至于容器的主题,您几乎绝不应该向容器施加指针。如果需要容器,只需创建一个实例:
First of all you should seldom use explicit memory management in modern C++. Use smart pointers like
std::unique_ptr
with ownership semantics.As for your problem, you can do it in roughly the same way, but kind of opposite instead: Auto-deduction of the type in the declaration, and specify the type in the
new
expression:Or using unique pointers:
As for the subject of containers, you should almost never make pointers to containers. If you need a container, just create an instance of it:
这是您可以写
在C ++中的新
(警告,测试)用法:
请注意,我说您可以写它,而不是您应该写它。这是高度违反直觉的抗异错C ++,绝对不应让生产代码接近。尝试危险事物很有趣,但是有趣的是有趣,而且工作是工作。
Here's how you could write
New
in C++ (warning, lightly tested)Usage:
Note, I said that you could write it, not that you should write it. This is highly counter-intuitive, anti-idiomatic C++ that should never be let near production code. It's fun to experiment with dangerous things, but fun is fun and work is work.
这是我的
safe_new
和safe_delete
variant:哪个有效,但不像我喜欢的
审美方式:
带有参数的新来,但没有明确的类型:
请注意,这不是一个确切的答案 - 更好的答案将是:
Here is my
safe_new
andsafe_delete
variant:Which works but is not as aesthetes as I like
Usage:
Call new with parameters but without explicit type:
Please note this is not an exact answer - A better answer would be something like:
首先,C ++和C#Sharp中
新
的含义不同。使用C#,每当您尝试调用构造函数时,都需要使用new
。但是,对于C ++,您只应使用new
在尝试创建一个生命周期的对象时不受其创建的范围的限制。即使您确实需要手动管理对象寿命,您应该考虑使用智能指针。因此,C#中的该代码:
要在C ++中重写,您确实不应使用
new
。相反,它应该像:现在回到您的问题一样,您想知道第二行,是否可以重新分配
ints
而无需输入其类型。虽然此示例中的类型很短,但如果类型实际上很长,可能会很详尽。您可以使其短的一种方法是创建一个可以创建任何对象的助手函数:
它的作用就是将所有参数转发到
return typepe
的构造函数。然后,您可以像以下内容一样使用它:但是,
exltype(foo)
仍然感觉冗长且多余。要进一步缩短它,您可以创建另一个功能,以foo
作为参考,然后在该函数中执行创建
:现在您的代码将变为:
First, the meanings of
new
in C++ and C# sharp are different. With C#, you would need to usenew
whenever you try to invoke a constructor. However with C++, you should only ever usenew
when you are trying to create an object whose lifetime is not limited by the scope it was created in. And even if you do need to manage object lifetime manually, you should consider using smart pointer instead.So this code in C#:
To rewrite it in C++, you really should not use
new
. Instead it should be written like:Now back to your problem, you were wondering at the second line, if you can reassign
ints
without typing out its type. While the type in this example is quite short, it could be quite verbose if the type was actually pretty long.One way you can make it shorter is by creating a helper function that can create any object:
What it does is simply forward all the arguments to the
ReturnType
's constructor. You can then use it like:However, the
decltype(foo)
still feels verbose and redundant. To further shorten it, you can create another function that takes infoo
as a reference, then do thecreate
inside that function:Now your code would become: