gpt4 book ai didi

c++ - 初始化和赋值

转载 作者:太空宇宙 更新时间:2023-11-04 12:28:24 25 4
gpt4 key购买 nike

我有一些“遗留”代码(我无法更改,但需要添加)看起来像这样:

template<typename T> T Foo(T& target)
{
//Assign to 'target', but never read from it before that.
//Also, 'target' is going to be a POD-type.

target = T();

return target;
}

int main()
{
float value = Foo(value);
}

这感觉不安全(即,确保 target 在使用之前从未分配给它),这种接口(interface)是否还有其他潜在的致命问题?

最佳答案

嗯..如果你做代码:

T value;

然后 value 将得到调用它的构造函数。老实说,模板看起来就像构造函数被调用了两次。

此外,如果 T 只是普通的旧数据,则不会发生致命的问题...

您到底担心发生什么?

关于c++ - 初始化和赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/720049/

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