本文介绍了其中哪些编译器有一个bug,根据标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下源代码:

#include <memory>
#include <iostream>

using namespace std;

struct concept
{
    virtual void perform() = 0;
};


struct model : concept, enable_shared_from_this<model>
{
    void perform() override {
        cout << "my pointer is " << shared_from_this().get() << endl;
    }
};

int main(int argc, const char * argv[])
{
    // shared_ptr<concept> concept_ptr = make_shared<model>();
    shared_ptr<concept> concept_ptr { new model };
    concept_ptr->perform();
    return 0;
}

在 gcc ,此代码编译并将内部 weak_ptr 与模型的地址相关联。

Compiling under gcc, this code compiles and associates the internal weak_ptr with the address of model.

clang 代码将无法编译(错误消息包含在结尾)

Under clang the code will not compile (error message included at the end)

您用 shared_ptr< concept>替换 concept_ptr 的初始化。

这是正确的吗?

编辑:

我的clang版本是 Xcode p>

My version of clang is the one that ships with Xcode:

$ clang --version
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix

只是想感谢大家的贡献。
如果你有兴趣,我想这样做的原因是我想要一个不透明的接口的实现与共享句柄语义。一些实现(异步的)要求回调对象确保实现对象仍然存在(争论 shared_from_this 和 weak_ptr :: lock )。其他实现不需要这一点。我想避免使用 enable_shared_from_this<> 基类来阻止概念(public interface),因为它将接口的实现与已知的邪恶联系起来。

Just wanted to say thanks to everyone for contributing.If you're interested, the reason I want to do this is that I want an opaque interface to an implementation with shared-handle semantics. Some implementations (async ones) require that callback objects ensure that the implementation object still exists (argues for shared_from_this and weak_ptr::lock). Other implementations do not require this. I wanted to avoid encumbering the concept (public interface) with the enable_shared_from_this<> base class, since that couples implementation with interface - a known evil.

在大多数情况下,使用make_shared创建实现对象是合理的。在很少需要自定义析构函数的情况下,以下似乎是可移植的:

In most cases, it's reasonable to use make_shared to create the implementation object. In rarer cases that require a custom destructor, the following seems portable:

    auto concept_ptr = static_pointer_cast<concept>(shared_ptr<model> {
                                                        new model ,
                                                        [](model* self) {
                                                            // some_deletion_operation on self;
                                                        } });

附录:
clang上的错误讯息:

appendix:error message on clang:

In file included from /Users/richardh/Documents/dev/Scratchpad/tryit/tryit/try2.cpp:1:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:4013:35: error: no viable overloaded '='
            __e->__weak_this_ = *this;
            ~~~~~~~~~~~~~~~~~ ^ ~~~~~
...etc...


推荐答案

我理解libstdc ++在这里更贴近标准。

I understand that libstdc++ follows the standard more closely here.

关于

shared_ptr<T> shared_from_this();
shared_ptr<const T> shared_from_this() const;

/ p>

both N3337 §20.7.2.4 (7) and N3936 §20.8.2.5 (7) only require

没有要求命名为 shared_ptr 拥有& t 实际上必须是 shared_ptr< T> 或 shared_ptr< A_to_T_Convertible> 。

There is no requirement named that one shared_ptr owning &t actually has to be a shared_ptr<T> or shared_ptr<A_to_T_Convertible>.

这个函数是该类功能的核心。

And that very function is the core of that class' functionality.

因此,给定 Tp 作为 enabled_shared_from_this 和 Tp1 作为拥有 shared_ptr , is_convertible< Tp1,Tp> :: value == true ,更不用说标准不需要 is_same :: value == true ,对于相应的指针也是如此。

Thus, given Tp as the actual param of the enabled_shared_from_this and Tp1 as the actual parameter of that owning shared_ptr, is_convertible<Tp1, Tp>::value == true, let alone is_same<Tp1, Tp>::value == true, is not required by the standard, same for respective pointers.

确实,使用libc ++的clang ++的完整输出具有

And indeed, the full output of clang++ using libc++ has

/usr/local/bin/../include/c++/v1/memory:3997:35: error: no viable overloaded '='
                __e->__weak_this_ = *this;
                ~~~~~~~~~~~~~~~~~ ^ ~~~~~
/usr/local/bin/../include/c++/v1/memory:4035:5: note: in instantiation of
      function template specialization
      'std::__1::shared_ptr<concept>::__enable_weak_this<model>' requested here
    __enable_weak_this(__p);
    ^
[...]enable_shared.cxx:34:25: note: in instantiation
      of function template specialization
      'std::__1::shared_ptr<concept>::shared_ptr<model>' requested here
    shared_ptr<concept> model_ptr1(new model);
                        ^
/usr/local/bin/../include/c++/v1/memory:4942:15: note: candidate function not
      viable: no known conversion from 'std::__1::shared_ptr<concept>' to 'const
      std::__1::weak_ptr<model>' for 1st argument
    weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
              ^
/usr/local/bin/../include/c++/v1/memory:4953:15: note: candidate function not
      viable: no known conversion from 'std::__1::shared_ptr<concept>' to
      'std::__1::weak_ptr<model>' for 1st argument
    weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
              ^
/usr/local/bin/../include/c++/v1/memory:4949:9: note: candidate template
      ignored: could not match 'weak_ptr' against 'shared_ptr'
        operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
        ^
/usr/local/bin/../include/c++/v1/memory:4960:9: note: candidate template
      ignored: could not match 'weak_ptr' against 'shared_ptr'
        operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
        ^
/usr/local/bin/../include/c++/v1/memory:4967:13: note: candidate template
      ignored: disabled by 'enable_if' [with _Yp = concept]
            is_convertible<_Yp*, element_type*>::value,
            ^

所以libc ++这里想要

So libc++ here wants

is_convertible<Tp1* /*= Base* = concept**/, Tp* /*= Derived* = model* */>

这当然在这里失败,运行时 * this shared_ptr< Tp1> 将是 dynamic_cast

which of course fails here, that the run-time *this of that very shared_ptr<Tp1> would be dynamic_cast-able to Tp* is out of ansatz here.

从这个角度来看,它也清楚了为什么 shared_ptr< concept> concept_ptr = make_shared< model>(); 不会受到影响;在 rhs 上有一个 shared_ptr< Tp / * = derived = model * /> 构造函数, code> ptr is_convertible 成立。

From this perspective, it's also clear why shared_ptr<concept> concept_ptr = make_shared<model>(); doesn't suffer from that; on the rhs there is a shared_ptr<Tp /* = derived = model */> constructor and for that ptr is_convertible holds.

libstdc ++不会受此影响,因为它传递参数 ( weak_ptr )中的 shared_ptr * = Derived = model * /> 分配,而不是构造中的 shared_ptr 。

libstdc++ doesn't suffer from this, because it passes the argument, thus its type (= Derived = model), of the shared_ptr<Tp1 /* = Base = concept*/> constructor down to the internal weak_ptr<T /*= Derived = model*/> assignment, not the shared_ptr in construction.

  template<typename _Tp, _Lock_policy _Lp>
    class __shared_ptr
{

template<typename _Tp1>
explicit __shared_ptr(_Tp1* __p)
    : _M_ptr(__p), _M_refcount(__p)
{
  __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
  static_assert( !is_void<_Tp1>::value, "incomplete type" );
  static_assert( sizeof(_Tp1) > 0, "incomplete type" );
  __enable_shared_from_this_helper(_M_refcount, __p, __p);
}

  template<typename _Tp, _Lock_policy _Lp>
    class __enable_shared_from_this
    {

private:
  template<typename _Tp1>
void
_M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept
{ _M_weak_this._M_assign(__p, __n); }


  template<typename _Tp1>
friend void
__enable_shared_from_this_helper(const __shared_count<_Lp>& __pn,
                 const __enable_shared_from_this* __pe,
                 const _Tp1* __px) noexcept
{
  if (__pe != 0)
    __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
}

我的观点;

@ Richard Hodges:+1,非常有趣的主题

@Richard Hodges: +1, very interesting topic

这篇关于其中哪些编译器有一个bug,根据标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 05:59