Templates 允许在唯一的\u ptr deleter中编写具有optionnal trait typedef类指针的模板类的规则是什么
*unique_ptr*管理可选Templates 允许在唯一的\u ptr deleter中编写具有optionnal trait typedef类指针的模板类的规则是什么,templates,template-specialization,unique-ptr,enable-if,Templates,Template Specialization,Unique Ptr,Enable If,*unique_ptr*管理可选typedef foo_type指针的方式在删除器中使我惊讶。我查看了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 Res
typedef foo_type指针的方式代码>在删除器中使我惊讶。我查看了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;
};
//此帮助程序将名为type的类型定义为Traits::type(如果存在)
//否则它被定义为隐式的
模板<类隐式,类特征>
类解析类型{
类包装{
公众:
WrapInt(int);
};
模板
静态自动MyFn(int)->typename T::Type;
模板
静态自动MyFn(WrapInt)->隐式;
公众:
typedef decltype(MyFn(0))类型;
};
现在,如果我们能够写下:
结构A{
typedef无符号整数类型;
};
结构B{
};
typedef ResolveType::Type ResultA;//这将解析为无符号整数
typedef ResolveType::Type ResultB;//这将解析为int
令人惊讶的是,如果我们在ResolveType
中使用B,则格式错误的MyFn
,因为typename B::Type
不存在
所以我有三个问题:
1.这是一种标准行为还是一种仅在VisualStudio编译器中才能正常工作的非标准技巧?
2.如果这是正确的标准行为,那么黑魔法背后的规则是什么?
3.这是否与*enable_if*有关,因为我也很难理解它如何允许拒绝函数重载的逻辑,就像上面的例子一样,通过使签名无效
谢谢。在提问之前,我应该多搜索一下,我找到了我的答案:SFINAE+1“令人惊讶的是,在我们使用B的情况下,格式错误的MyFn……因为typename B::Type不存在”。
typedef ResolveType< int, A >::Type ResultA; // this will resolve to unsigned int
typedef ResolveType< int, B >::Type ResultB; // this will resolve to int