简体   繁体   English

从 C++ function 转换返回值

[英]casting a returned value from C++ function

I have following C++ code snippet我有以下 C++ 代码片段

inline std::vector<std::unique_ptr<xir::Tensor>> cloneTensorBuffer(
    const std::vector<const xir::Tensor*>& tensors) {
  auto ret = std::vector<std::unique_ptr<xir::Tensor>>{};
  auto type = xir::DataType::XINT;
  ret.reserve(tensors.size());
  for (const auto& tensor : tensors) {
    ret.push_back(std::unique_ptr<xir::Tensor>(xir::Tensor::create(
        tensor->get_name(), tensor->get_shape(), xir::DataType{type, 8u})));
  }
  return ret;
}

I am not clear about the expression:我不清楚表达方式:

std::unique_ptr<xir::Tensor>(xir::Tensor::create(
            tensor->get_name(), tensor->get_shape(), xir::DataType{type, 8u}))

Is the expression casting the value returned by xir::Tensor::create() to std::unique_ptr<xir::Tensor?表达式是否将 xir::Tensor::create() 返回的值转换为 std::unique_ptr<xir::Tensor? I am confused since the C++ casting syntax is (type)expression Can someone explain please.我很困惑,因为 C++ 转换语法是(类型)表达式有人可以解释一下吗?

regards, -sunil puranik问候,-sunil puranik

This code takes a vector of raw pointers to Tensor objects, and returns a new vector of unique pointers to a new set of Tensor objects.此代码采用指向张量对象的原始指针向量,并返回一个新的唯一指针向量,指向一组新的张量对象。 Doesn't look like any explicit casting is taking place.看起来没有进行任何明确的强制转换。

std::unique_ptr<xir::Tensor>( // create unique pointer to new Tensor object
    xir::Tensor::create(
        tensor->get_name(),  // created with name and shape of the
        tensor->get_shape(), // tensor pointed at in the original vector
        xir::DataType{type, 8u} // and with unsigned 8-bit XINT datatype
    )
)

Regarding casting - please don't consider eg (int)foo to be C++-style casting - that's legacy C-style casting.关于铸造 - 请不要将例如(int)foo视为 C++ 风格的铸造 - 那是传统的 C 风格的铸造。 Amongst other things it's hard to find by searching a codebase!除其他外,通过搜索代码库很难找到!

C++ has: C++ 具有:

  • static_cast
  • dynamic_cast
  • reinterpret_cast
  • const_cast
  • implicit conversion隐式转换

There's more detail on this at cplusplus.com .在 cplusplus.com上有更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM