c++ - 在没有 RValue 隐式转换的情况下正确实现

标签 c++ shared-ptr rvalue rvalue-reference

我遇到了 RValue 不允许隐式转换的问题。我的问题是什么实现更好地“绕过”这个限制?

下面是说明问题的示例代码:

template<typename myVal>
class ITestClass
{
public:
  virtual void myFunc(myVal item) = 0;
  virtual myVal myFunc1() = 0;
};

class CTestClass : public ITestClass<int>
{
public:
  void myFunc(int item) { }
  int myFunc1() { return 0; }
};

template <typename T>
inline int CallFunction(std::shared_ptr< ITestClass<T> > ptrBase)
{
  return 0;
}

inline std::shared_ptr< ITestClass<int> > GetBase()
{
  return std::make_shared<CTestClass>();
}


std::shared_ptr< ITestClass<int> > ptrBase = std::make_shared<CTestClass>();
std::shared_ptr< CTestClass > ptrDerived = std::make_shared<CTestClass>();
CallFunction(ptrBase); // WORKS
CallFunction(GetBase()); // WORKS
CallFunction(std::make_shared<CTestClass>()); // ERROR
CallFunction(ptrDerived); // ERROR

所有可以使用 RValue 但函数需要基数且参数是派生的调用均失败。

选项 1

解决问题的选项 1:

CallFunction(std::static_pointer_cast< ITestClass<int> >(std::make_shared<CTestClass>()));
CallFunction(std::static_pointer_cast< ITestClass<int> >(ptrDerived));

此选项要求用户在调用函数之前将派生类转换为基类。这违背了一些目的,因为它要求调用者知道转换的实际基类型(也就是它是什么具体模板实例化基类型)。

选项 2

解决问题的选项 2: (修改template和CallFunction一些)

template<typename myVal>
class ITestClass
{
public:
  typedef myVal class_data_type;

  virtual void myFunc(myVal item) = 0;
  virtual myVal myFunc1() = 0;
};

class CTestClass : public ITestClass<int>
{
public:
  void myFunc(int item) { }
  int myFunc1() { return 0; }
};

template <typename T>
inline int CallFunction(std::shared_ptr<T> ptrBase)
{
  static_assert(std::is_base_of<ITestClass<typename T::class_data_type>, T>::value, "Class needs to derive from ITestClass"); // some example of type checking

  return 0;
}

CallFunction(std::make_shared<CTestClass>()); // now works as normal
CallFunction(ptrDerived); // now works as normal

我更喜欢选项 2,因为调用者不知道当前对 RValue 施加的限制,但我不确定是否有足够的类型检查 static_asserts 可以在有人传递错误参数时消除困惑。

问题

  1. 您认为选项 2 有什么问题吗?还是选项 1 仍然是更好的路线?

  2. 使用 SFINAE 有没有办法清理类型安全?

最佳答案

好吧,它与右值没有任何关系,而是与模板参数推导失败有关。

模板参数匹配非常非常直接,就像一个简单的模式匹配。

下面是解决它的一种方法,在接口(interface)类中使用 typedef:

#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
namespace our = boost;

template<typename myVal>
class ITestClass
{
public:
  typedef myVal ValType;

  virtual void myFunc(myVal item) = 0;
  virtual myVal myFunc1() = 0;
};

class CTestClass : public ITestClass<int>
{
public:
  void myFunc(int item) { }
  int myFunc1() { return 0; }
};

template <typename T>
inline int CallFunctionAux(
    our::shared_ptr< ITestClass<T> > ptrBase
    )
{
  return 0;
}

template< class T >
inline int CallFunction( our::shared_ptr< T > ptrBase )
{
  return CallFunctionAux< typename T::ValType >( ptrBase );
}

inline our::shared_ptr< ITestClass<int> > GetBase()
{
  return our::make_shared<CTestClass>();
}


int main()
{
    our::shared_ptr< ITestClass<int> > ptrBase = our::make_shared<CTestClass>();
    our::shared_ptr< CTestClass > ptrDerived = our::make_shared<CTestClass>();
    CallFunction(ptrBase); // WORKS
    CallFunction(GetBase()); // WORKS
    CallFunction(our::make_shared<CTestClass>()); // WORKS
    CallFunction(ptrDerived); // WORKS
}

干杯,

关于c++ - 在没有 RValue 隐式转换的情况下正确实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4831192/

相关文章:

C++0x const RValue 引用作为函数参数

c++ - 将 vector 作为函数的引用传递

c++ - 我可以将 move 的变量标记为不再可用,并在使用它时收到编译器警告吗?

c++ - OOP:派生子类中数据库连接的正确类设计?

c++ - 当使用shared_ptr创建实例时,指针实例变量会发生什么情况?

C++ - shared_ptr<vector<T>> 与 vector<shared_ptr<T>>

c++ - 为什么右值没有地址?

c++ - 如何访问继承成员

c++ - 从 C++ 程序创建 XML 文件

C++11 标准决定 "shared_ptr(const weak_ptr<Y>& r) Throws bad_weak_ptr"