12

考虑这个最小的例子:

#include <memory>

struct B {
  typedef std::shared_ptr<B> Ptr;
};

struct A {
  operator B::Ptr() { // type conversion operator                  <----+
    return std::make_shared<B>();  //                                   |
  }                                //                                   |
};                                 //                                   |
                                   //                                   |
int main() {                       //                                   |
  A* a = new A;                    //                                   |
  B::Ptr{*a}; // copy construction from a's implicit cast to B::Ptr ----+ 
}

这种无害的 a 副本构造shared_ptr<B> 在 g++ 4.6.3 x86_64-linux-gnu 上非常失败,但似乎适用于g++ 4.5(请注意,较新的版本会中断,而较旧的版本可以工作!)。从我可以从错误(见下文)中看出,g++ 4.6 似乎是A按值传递,而不是按(r 或 l)引用传递。

所以,问题是,哪个是正确的,哪个是错误的?这种行为应该失败吗?如果是这样,为什么?
据我了解转换规则,此时B::Ptr 应该尝试隐式转换,对吗?


注意:我将此示例简化为纯粹的技术问题,并且此代码对任何生产系统都没有意义。

这是精确的错误:

shp.cpp: In function ‘int main()’:
shp.cpp:17:12: error: no matching function for call to ‘std::shared_ptr<B>::shared_ptr(<brace-enclosed initializer list>)’
shp.cpp:17:12: note: candidates are:
/usr/include/c++/4.6/bits/shared_ptr.h:315:2: note: template<class _Alloc, class ... _Args> std::shared_ptr::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...)
/usr/include/c++/4.6/bits/shared_ptr.h:266:17: note: constexpr std::shared_ptr<_Tp>::shared_ptr(std::nullptr_t) [with _Tp = B, std::nullptr_t = std::nullptr_t]
/usr/include/c++/4.6/bits/shared_ptr.h:266:17: note:   no known conversion for argument 1 from ‘A’ to ‘std::nullptr_t’
/usr/include/c++/4.6/bits/shared_ptr.h:258:2: note: template<class _Tp1, class _Del> std::shared_ptr::shared_ptr(std::unique_ptr<_Up, _Ep>&&)
/usr/include/c++/4.6/bits/shared_ptr.h:253:2: note: template<class _Tp1> std::shared_ptr::shared_ptr(std::auto_ptr<_Tp1>&&)
/usr/include/c++/4.6/bits/shared_ptr.h:248:11: note: template<class _Tp1> std::shared_ptr::shared_ptr(const std::weak_ptr<_Tp1>&)
/usr/include/c++/4.6/bits/shared_ptr.h:236:2: note: template<class _Tp1, class> std::shared_ptr::shared_ptr(std::shared_ptr<_Tp1>&&)
/usr/include/c++/4.6/bits/shared_ptr.h:226:7: note: std::shared_ptr<_Tp>::shared_ptr(std::shared_ptr<_Tp>&&) [with _Tp = B, std::shared_ptr<_Tp> = std::shared_ptr<B>]
/usr/include/c++/4.6/bits/shared_ptr.h:226:7: note:   no known conversion for argument 1 from ‘A’ to ‘std::shared_ptr<B>&&’
/usr/include/c++/4.6/bits/shared_ptr.h:218:2: note: template<class _Tp1, class> std::shared_ptr::shared_ptr(const std::shared_ptr<_Tp1>&)
/usr/include/c++/4.6/bits/shared_ptr.h:206:2: note: template<class _Tp1> std::shared_ptr::shared_ptr(const std::shared_ptr<_Tp1>&, _Tp*)
/usr/include/c++/4.6/bits/shared_ptr.h:184:2: note: template<class _Deleter, class _Alloc> std::shared_ptr::shared_ptr(std::nullptr_t, _Deleter, _Alloc)
/usr/include/c++/4.6/bits/shared_ptr.h:165:2: note: template<class _Tp1, class _Deleter, class _Alloc> std::shared_ptr::shared_ptr(_Tp1*, _Deleter, _Alloc)
/usr/include/c++/4.6/bits/shared_ptr.h:146:2: note: template<class _Deleter> std::shared_ptr::shared_ptr(std::nullptr_t, _Deleter)
/usr/include/c++/4.6/bits/shared_ptr.h:129:2: note: template<class _Tp1, class _Deleter> std::shared_ptr::shared_ptr(_Tp1*, _Deleter)
/usr/include/c++/4.6/bits/shared_ptr.h:112:11: note: template<class _Tp1> std::shared_ptr::shared_ptr(_Tp1*)
/usr/include/c++/4.6/bits/shared_ptr.h:103:7: note: std::shared_ptr<_Tp>::shared_ptr(const std::shared_ptr<_Tp>&) [with _Tp = B, std::shared_ptr<_Tp> = std::shared_ptr<B>]
/usr/include/c++/4.6/bits/shared_ptr.h:103:7: note:   no known conversion for argument 1 from ‘A’ to ‘const std::shared_ptr<B>&’
/usr/include/c++/4.6/bits/shared_ptr.h:100:17: note: constexpr std::shared_ptr<_Tp>::shared_ptr() [with _Tp = B]
/usr/include/c++/4.6/bits/shared_ptr.h:100:17: note:   candidate expects 0 arguments, 1 provided
4

1 回答 1

5

在当前版本的标准下,代码不正确(我正在查看后标准草案 n3376)。

列表初始化的规则指定:

13.3.1.7 通过列表初始化 [over.match.list] 进行初始化

1 - 当非聚合类类型的对象T被列表初始化时[...]:

  • 如果没有找到可行的初始化列表构造函数,则再次执行重载决议,其中候选函数是类的所有构造函数,T参数列表由初始化列表的元素组成。

但是,当重载决议应用于采用B::Ptr单个参数的复制构造函数时const std::shared_ptr<B> &,参数列表是(*a),由一个lvalue A类型的元素组成;不允许重载决议考虑转换函数A::operator B::Ptr

13.3.3.1 隐式转换序列 [over.best.ics]

4 - 但是,当考虑构造函数或用户定义的转换函数的参数时,它是 13.3.1.7 [...] 的候选 [...] 当初始化器列表只有一个元素并且转换到某个类 X或对(可能是 cv 限定的)X 的引用被认为是 X [...] 的构造函数的第一个参数,仅考虑标准转换序列和省略号转换序列。

所以g++-4.6拒绝这个代码是正确的;g++-4.7.2 不幸地接受了它,这是不正确的。

编写此代码的正确方法是使用直接初始化 ( B::Ptr(*a)) 或static_cast<B::Ptr>.

允许转换的限制可以追溯到论文n2672,尽管在该论文中第 13.3.3.1p4 段仅适用于用户定义的转换函数的参数在缺陷 978中添加了对构造函数的附加限制:

978. 拷贝初始化的指定不正确

13.3.3.1 [over.best.ics] 第 4 段说,
[...]
这不太正确,因为这适用于构造函数参数,而不仅仅是用户定义的转换函数的参数。

13.3.3.1p4 的当前措辞可以追溯到开创性缺陷 84,它引入了“普通法规则,即仅调用单个用户定义的转换来进行隐式转换”。

我对这个答案有点不安;我问过是否可以通过列表初始化调用用户定义的转换函数?看看是否有人可以在这里澄清标准的意图。

于 2012-09-27T21:10:21.683 回答