【问题标题】:Proper Implementation in the midst of no RValue implicit conversion在没有 RValue 隐式转换的情况下正确实现
【发布时间】:2011-01-28 17:26:47
【问题描述】:

我遇到了 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: (修改模板和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 有没有办法清理类型安全?

【问题讨论】:

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


    【解决方案1】:

    嗯,和右值没有任何关系,而是模板参数推导失败。

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

    以下是解决它的一种方法,在接口类中使用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
    }
    

    干杯,

    【讨论】:

    • @BabelFish:差不多,但又不一样。没有断言,没有演员。如果使用不好的类型调用,则只是编译错误。 ;-)
    • 因此,如果 typename T::ValType 无法解决,您的方法是删除 static_assert 并出现编译问题。当传递了错误的类型时,我的选项 2 也会给出相同的错误,并且我没有看到我的类型在哪里进行转换,或者我错过了什么?
    • @BabelFish:你的代码什么也没做。可能是您打算指出上述解决方案?在那种情况下,这确实是您的选择2,我误解了您的意思。干杯,
    • hmm..“选项 2”下显示的代码包含“typedef myVal class_data_type;”在模板内,新转换的“CallFunction”现在采用与您的“std::shared_ptr ptrBase”相同的参数。我的空函数调用static_assert,你的调用另一个模板函数。如果 typedef 不存在,这两种实现都会失败。您是在查看选项 2 代码还是顶部的代码?再次感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 2020-04-29
    • 2019-11-29
    • 2011-12-14
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多