【问题标题】:Conversion operator implemented with static_cast使用 static_cast 实现的转换运算符
【发布时间】:2012-03-22 09:17:54
【问题描述】:

我在here 提出的问题之后提出了这个问题。

要点很简单。假设您有两个此类:

template < class Derived >
class Base {
...
operator const Derived&() const {
    return static_cast< const Derived& >(*this);
  }
...
};

class Specialization : public Base<Specialization> {
...
};

那么假设你有一个像这样的类型转换:

template < class T >
functionCall( const Base<T>& param) {
  const T & val(param);
  ...
}

问题是:这种转换的符合标准的行为应该是什么?

应该和const T &amp; val(static_cast&lt;const T &amp;&gt; (param) ) 一样还是应该递归迭代直到堆栈溢出?请注意,我获得了使用 GNU g++ 编译的第一个行为和使用 Intel icpc 编译的第二个行为。

我已经尝试查看标准(关于 static_cast 的第 5.9 节和关于转换的第 12.3 节),但由于我缺乏经验,我无法找出答案。

非常感谢任何抽出时间帮助我解决此问题的人。

【问题讨论】:

  • 相关部分将是关于重载解决方案的部分。
  • 如果您能满足我的好奇心 - 为什么要在 Base 中使用转换运算符?
  • @Tony Delroy: 1) 比显式调用类方法更简洁。2) 我在网络上发现了几次这种代码,同时试图获得一些关于 CRTP 的知识,请参阅例如here 3) 在发现这种奇怪的行为之后,我真的很好奇哪个应该是正确的:-)

标签: c++ standards crtp static-cast conversion-operator


【解决方案1】:

查看 n3337 中的 [expr.static.cast](标准之后的第一个工作草案):

2/ 类型为“cv1 B”的左值,其中B 是类类型,可以转换为类型“引用 cv2 D”,其中D是从B 派生的类(第 10 条),如果存在从“指向D”的有效标准转换为“指向B”的指针[...]

4/ 否则,如果声明 T t(e); 格式正确,则表达式 e 可以使用 static_cast 形式的 static_cast&lt;T&gt;(e) 显式转换为类型 static_cast&lt;T&gt;(e) , 对于一些发明的临时变量t [..]

因此,我认为 gcc 的行为是正确的,即表达式:

static_cast<Derived const&>(*this)

不应递归调用operator Derived const&amp; () const。

我从 Otherwise 关键字的存在中推断出这一点,这意味着规则的排序。规则 2/ 应该在规则 4/ 之前尝试。

【讨论】:

    【解决方案2】:

    不推荐使用隐式转换运算符。在 C++11 中,您不仅可以将关键字 explicit 添加到单参数构造函数,还可以添加到转换运算符。对于 C++03 代码,您可以使用显式命名的转换函数,例如 self() 或 down_cast()。

    此外,您似乎正在为 CRTP 使用 Base 类,即启用静态多态性。这意味着您必须在 编译时 知道您正在调用哪个特定的 Derived 类。因此,除了实现 CRTP 接口外,您不必在任何公共代码中使用 const Base&amp; 引用。

    在我的项目中,我有一个类模板enable_crtp:

    #include <type_traits>
    #include <boost/static_assert.hpp>
    
    template
    <
            typename Derived
    >
    class enable_crtp
    {
    public:
            const Derived& self() const
            {
                    return down_cast(*this);
            }
    
            Derived& self()
            {
                    return down_cast(*this);
            }
    
    protected:
            // disable deletion of Derived* through Base* 
            // enable deletion of Base* through Derived*
            ~enable_crtp()
            {
                    // no-op
            }
    
    private:
            // typedefs
            typedef enable_crtp Base;
    
            // cast a Base& to a Derived& (i.e. "down" the class hierarchy)
            const Derived& down_cast(const Base& other) const
            {
                  BOOST_STATIC_ASSERT((std::is_base_of<Base, Derived>::value));
                  return static_cast<const Derived&>(other);
            }
    
            // cast a Base& to a Derived& (i.e. "down" the class hierarchy)
            Derived& down_cast(Base& other)
            {
            // write the non-const version in terms of the const version
            // Effective C++ 3rd ed., Item 3 (p. 24-25)
            return const_cast<Derived&>(down_cast(static_cast<const Base&>(other)));
            }
    };
    

    此类由任何 CRTP 基类 ISomeClass 私有派生,如下所示:

    template<typename Impl>
    class ISomeClass
    :
        private enable_crtp<Impl>
    {
    public:
        // interface to be implemented by derived class Impl
        void fun1() const
        {
            self().do_fun1();
        }
    
        void fun2()
        {
            self().do_fun2()
        }
    
    protected:
        ~ISomeClass()
        {}  
    };
    

    各种派生类可以用自己特定的方式实现这个接口,如下所示:

    class SomeImpl
    :
        public ISomeClass<SomeImpl>
    {
    public:
        // structors etc.
    
    private:
        // implementation of interface ISomeClass
    
        friend class ISomeClass<SomeImpl>;
    
        void do_fun1() const
        {
            // whatever
        }
    
        void do_fun2() 
        {
            // whatever
        }
    
        // data representation
        // ...
    };
    

    调用class SomeImpl 的fun1 的外部代码将在class enable_crtp 中被委派给self() 的适当const 或非const 版本,并且在down_casting 之后将调用实现do_fun1。使用一个体面的编译器,所有的间接都应该被完全优化掉。

    注意:ISomeClass 和 enable_crtp 的受保护析构函数使代码对试图通过基指针删除 SomeImpl* 对象的用户安全。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多