将 std::shared_ptr<T> 传递给采用 std::shared_ptr<const T> 的函数?

Ind*_*ox3 9 c++ shared-ptr c++11 c++14

我有一个函数需要共享一个参数的所有权,但不修改它。我已将参数设为 shared_ptr<const T> 以清楚地传达此意图。

template <typename T>
void func(std::shared_ptr<const T> ptr){}
Run Code Online (Sandbox Code Playgroud)

我想将此函数与 shared_ptr 调用到非常量 T。例如:

auto nonConstInt = std::make_shared<int>();
func(nonConstInt);
Run Code Online (Sandbox Code Playgroud)

但是,这会在 VC 2017 上生成编译错误:

error C2672: 'func': no matching overloaded function found
error C2784: 'void func(std::shared_ptr<const _Ty>)': could not deduce template argument for 'std::shared_ptr<const _Ty>' from 'std::shared_ptr<int>'
note: see declaration of 'func'
Run Code Online (Sandbox Code Playgroud)

有没有办法让这项工作没有:

  • 修改对 func 的调用。这是更大的代码重构的一部分,我不想在每个调用站点都使用 std::const_pointer_cast 。
  • 定义 func 的多个重载,因为这似乎是多余的。

我们目前正在根据 C++14 标准进行编译,如果有帮助,我们计划很快迁移到 C++17。

Yak*_*ont 5

template <typename T>
void cfunc(std::shared_ptr<const T> ptr){
  // implementation
}
template <typename T>
void func(std::shared_ptr<T> ptr){ return cfunc<T>(std::move(ptr)); }
template <typename T>
void func(std::shared_ptr<const T> ptr){ return cfunc<T>(std::move(ptr)); }
Run Code Online (Sandbox Code Playgroud)

这与cbegin工作方式相匹配,并且“过载”是几乎零成本的琐碎转发器。


Ind*_*ox3 1

感谢您的回复。

我最终以稍微不同的方式解决了这个问题。我将函数参数更改为 ashared_ptr到 any T,以便它允许 const 类型,然后我将std::enable_if模板限制为我关心的类型。(就我vector<T>而言const vector<T>

调用站点不需要修改。当使用两者调用shared_ptr<const T>并且shared_ptr<T>不需要单独的重载时,该函数将编译。

下面是在 VC、GCC 和 clang 上编译的完整示例:

#include <iostream>
#include <memory>
#include <vector>

template<typename T>
struct is_vector : public std::false_type{};

template<typename T>
struct is_vector<std::vector<T>> : public std::true_type{};

template<typename T>
struct is_vector<const std::vector<T>> : public std::true_type{};

template <typename ArrayType,
         typename std::enable_if_t<is_vector<ArrayType>::value>* = nullptr>
void func( std::shared_ptr<ArrayType> ptr) {
}

int main()
{
    std::shared_ptr< const std::vector<int> > constPtr;
    std::shared_ptr< std::vector<int> > nonConstPtr;
    func(constPtr);
    func(nonConstPtr);
}
Run Code Online (Sandbox Code Playgroud)

唯一的缺点是 的非常量实例化func将允许在传入的 ptr 上调用非常量方法。就我而言,仍然会生成编译错误,因为有一些对 const 版本的调用func,并且两个版本都来自同一模板。