根据标准,哪些编译器有错误?

时间:2014-07-20 13:44:51

标签: c++ gcc c++11 clang

给出以下源代码:

#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_ptrmodel的地址相关联。

clang下,代码将无法编译(最后包含错误消息)

如果您将concept_ptr的初始化替换为shared_ptr<concept> concept_ptr = make_shared<model>();,则会在两者上进行编译。

哪个是对的?

编辑:

我的clang版本是随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

EDIT2:

只想感谢大家的贡献。 如果您感兴趣,我想要这样做的原因是我想要一个带有共享句柄语义的实现的不透明接口。某些实现(异步的)要求回调对象确保实现对象仍然存在(shared_from_thisweak_ptr::lock的争论)。其他实现不需要这样。我想避免使用enable_shared_from_this<>基类来阻碍概念(公共接口),因为它将实现与接口耦合 - 一种已知的恶意。

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

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

附录: 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...    

1 个答案:

答案 0 :(得分:11)

我知道libstdc ++在这里更接近标准。

关于

的要求
shared_ptr<T> shared_from_this(); 
shared_ptr<const T> shared_from_this() const; 

N3337§20.7.2.4(7)和N3936§20.8.2.5(7)仅要求

  

enable_shared_from_this<T>应该是一个可访问的基类   T*this应为t类型的对象T的子对象。应该   至少有一个拥有shared_ptr的{​​{1}}实例p

没有要求指出一个&t拥有shared_ptr实际上必须是&tshared_ptr<T>

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

因此,将shared_ptr<A_to_T_Convertible>作为Tpenabled_shared_from_this的实际参数,作为拥有Tp1shared_ptr的实际参数,更不用说{ {1}},标准不需要,各个指针也一样。


实际上,使用libc ++的clang ++的完整输出有

is_convertible<Tp1, Tp>::value == true

所以这里的libc ++需要

is_same<Tp1, Tp>::value == true

当然在这里失败了,那个非常/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, ^ 的{​​{1}}的运行时is_convertible<Tp1* /*= Base* = concept**/, Tp* /*= Derived* = model* */> - *this - shared_ptr<Tp1>能够{ans}在这里。


从这个角度来看,也很明显为什么dynamic_cast不会遭受这种情况;在Tp*上有一个shared_ptr<concept> concept_ptr = make_shared<model>();构造函数,rhs shared_ptr<Tp /* = derived = model */>成立。


libstdc ++不会受此影响,因为它将ptr构造函数的参数,即其类型(= Derived = model)传递给内部{{1}分配,而不是构造中的is_convertible

https://github.com/mirrors/gcc/blob/master/libstdc%2B%2B-v3/include/bits/shared_ptr_base.h#L848

shared_ptr<Tp1 /* = Base = concept*/>

https://github.com/mirrors/gcc/blob/master/libstdc%2B%2B-v3/include/bits/shared_ptr_base.h#L858

weak_ptr<T /*= Derived = model*/>

https://github.com/mirrors/gcc/blob/master/libstdc%2B%2B-v3/include/bits/shared_ptr_base.h#L1459

shared_ptr

https://github.com/mirrors/gcc/blob/master/libstdc%2B%2B-v3/include/bits/shared_ptr_base.h#L1482

  template<typename _Tp, _Lock_policy _Lp>
    class __shared_ptr
{

我的观点;欢迎评论。

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