C++ 中指向 unique_ptr 函数参数的原始指针的用途是什么?
What is the purpose of a raw pointer to unique_ptr function parameter in C++?
浏览 TensorFlow C++ API 我偶然发现了一个奇怪的函数签名:
Status Create(
const FileSystemStoragePathSourceConfig & config,
std::unique_ptr< FileSystemStoragePathSource > *result
)
我不明白为什么 result
是指向 unique_ptr
的原始指针。因为通常当我们想将 unique_ptr
作为函数参数传递时,我们要么通过移动 (&&
) 或通过引用 &
或通过值来完成。我以前没有见过 unique_ptr
的原始指针的用例。
TensorFlow 由 Google 开发。 Google's C++ style guide 说:
All parameters passed by reference must be labeled const
.
这意味着所有输出参数必须是指针,而不是引用。这种风格不是惯用的 C++(标准库通过引用传递输出参数),但 Google 风格指南的支持者认为,将输出参数作为指针传递可以更清楚哪些参数是输入参数,哪些是输出参数。
浏览 TensorFlow C++ API 我偶然发现了一个奇怪的函数签名:
Status Create(
const FileSystemStoragePathSourceConfig & config,
std::unique_ptr< FileSystemStoragePathSource > *result
)
我不明白为什么 result
是指向 unique_ptr
的原始指针。因为通常当我们想将 unique_ptr
作为函数参数传递时,我们要么通过移动 (&&
) 或通过引用 &
或通过值来完成。我以前没有见过 unique_ptr
的原始指针的用例。
TensorFlow 由 Google 开发。 Google's C++ style guide 说:
All parameters passed by reference must be labeled
const
.
这意味着所有输出参数必须是指针,而不是引用。这种风格不是惯用的 C++(标准库通过引用传递输出参数),但 Google 风格指南的支持者认为,将输出参数作为指针传递可以更清楚哪些参数是输入参数,哪些是输出参数。