0

*unique_ptr* 在删除器中管理可选项的方式typedef foo_type pointer;让我感到惊讶。我查看了 Visual Studio 2012 的实现,并为示例剥离了一个小实现:

// this helper defined a type named Type as Traits::Type if it exists
// otherelse it is defined as Implicit
template < class Implicit, class Traits >
class ResolveType {
    class WrapInt {
    public:
        WrapInt( int );
    };

    template < class T >
    static auto MyFn( int ) -> typename T::Type;

    template < class T >
    static auto MyFn( WrapInt ) -> Implicit;

public:
    typedef decltype( MyFn<Traits>(0) ) Type;
};

现在,如果我们能够这样写: struct A { typedef unsigned int Type; }; 结构 B { };

typedef ResolveType< int, A >::Type ResultA; // this will resolve to unsigned int
typedef ResolveType< int, B >::Type ResultB; // this will resolve to int

令人惊讶的是,MyFn在我们在内部使用 B 的情况下,ResolveType由于typename B::Type不存在而形成的病态。

所以我有三个问题: 1. 这是标准行为还是非标准技巧只能在 Visual Studio 编译器中正常工作?2. 如果这是正确的标准行为,那么黑魔法背后的规则是什么?3.这是否与*enable_if*有关,因为我也很难理解它如何允许拒绝函数重载的逻辑,有点像上面的例子一样,通过使签名无效?

谢谢。

4

0 回答 0