类模板中的 typedef 的 SFINAE 失败是指另一个类模板中的 typedef

SFINAE failure with typedef in class template referring to typedef in another class template

本文关键字:typedef 另一个 失败 SFINAE      更新时间:2023-10-16

我一直在研究一种方法,以生成有关将其他类包装在C++中的类的编译时信息。在我将要问的问题的一个最小示例中,这样一个包装类:

  • 包含一个定义包装类类型的typedef WrappedType;和
  • 重载名为 IsWrapper 的结构模板,以指示它是一个包装类。

有一个名为 WrapperTraits 的结构模板,然后可用于确定包装类型层次结构的(非包装器)根类型。 例如,如果包装类是一个名为 Wrapper<T> 的类模板,则Wrapper<Wrapper<int>>的根类型将是int

在下面的代码片段中,我实现了一个名为 GetRootType<T> 的递归结构模板,该模板定义了一个typedef RootType,该模板将包装器的根类型T。给定的WrapperTraits定义只包含GetRootType定义的根类型,但实际上它会有一些额外的成员。为了测试它,我编写了一个采用int的普通函数f,以及一个重载的函数模板f,该模板采用以int作为其根类型的任意包装类。我使用 SFINAE 来区分它们,通过在函数模板的返回类型中使用std::enable_if来检查f参数的根类型是否int(如果f的参数不是包装器,则尝试确定其根类型将失败)。在我问我的问题之前,这里是代码片段:

#include <iostream>
#include <type_traits>

// Wrapper #######################################
template<class T>
struct Wrapper {typedef T WrappedType;};
template<class T, class Enable=void>
struct IsWrapper: std::false_type {};
template<class T>
struct IsWrapper<Wrapper<T> >: std::true_type {};

// WrapperTraits #######################################
template<
  class T,
  bool HasWrapper=
    IsWrapper<T>::value && IsWrapper<typename T::WrappedType>::value>
struct GetRootType {
  static_assert(IsWrapper<T>::value,"T is not a wrapper type");
  typedef typename T::WrappedType RootType;
};
template<class T>
struct GetRootType<T,true> {
  typedef typename GetRootType<typename T::WrappedType>::RootType RootType;
};
template<class T>
struct WrapperTraits {
  typedef typename GetRootType<T>::RootType RootType;
};

// Test function #######################################
void f(int) {
  std::cout<<"int"<<std::endl;
}
// #define ROOT_TYPE_ACCESSOR WrapperTraits  // <-- Causes compilation error.
#define ROOT_TYPE_ACCESSOR GetRootType    // <-- Compiles without error.
template<class T>
auto f(T) -> 
  typename std::enable_if<
    std::is_same<int,typename ROOT_TYPE_ACCESSOR<T>::RootType>::value
  >::type
{
  typedef typename ROOT_TYPE_ACCESSOR<T>::RootType RootType;
  std::cout<<"Wrapper<...<int>...>"<<std::endl;
  f(RootType());
}

int main() {
  f(Wrapper<int>());  
  return 0; 
}

这将正确编译(在此处尝试)并生成输出:

Wrapper<...<int>...>
int

但是,我已经使用GetRootType来确定调用std::enable_if中的根类型。如果我改用WrapperTraits来确定根类型(您可以通过更改ROOT_TYPE_ACCESSOR的定义来做到这一点),GCC 会产生以下错误:

test.cpp: In instantiation of ‘struct WrapperTraits<int>’:
test.cpp:49:6:   required by substitution of ‘template<class T> typename std::enable_if<std::is_same<int, typename WrapperTraits<T>::RootType>::value>::type f(T) [with T = int]’
test.cpp:57:15:   required from ‘typename std::enable_if<std::is_same<int, typename WrapperTraits<T>::RootType>::value>::type f(T) [with T = Wrapper<int>; typename std::enable_if<std::is_same<int, typename WrapperTraits<T>::RootType>::value>::type = void]’
test.cpp:62:19:   required from here
test.cpp:21:39: error: ‘int’ is not a class, struct, or union type
   bool HasWrapper=IsWrapper<T>::value && IsWrapper<typename T::WrappedType>::value>

我的问题是:C++标准中关于参数推导的规则解释了为什么使用 WrapperTraits会导致编译错误,而使用 GetRootType 则不会导致编译错误?请注意,我问这个问题的目的是能够理解为什么会发生这个编译错误。我对可以进行哪些更改以使其工作不太感兴趣,因为我已经知道将WrapperTraits的定义更改为此可以修复错误:

template<
  class T,
  class Enable=typename std::enable_if<IsWrapper<T>::value>::type>
struct WrapperTraits {
  typedef typename GetRootType<T>::RootType RootType;
};
template<class T>
struct WrapperTraits<T,typename std::enable_if<!IsWrapper<T>::value>::type> {
};

但是,如果有人能看到更优雅的fWrapperTraits书写方式,我会非常有兴趣看到它!

你问题的第一部分是为什么它失败了。答案是,在编译时级别,&&没有短路属性。这一行:

bool HasWrapper=IsWrapper<T>::value && IsWrapper<typename T::WrappedType>::value>

失败,因为第一个条件是false,但编译器尝试实例化第二部分,但T int,因此T::WrappedType不起作用。


为了回答你关于如何让它更容易的问题的第二部分,我认为以下内容应该让你很高兴:

#include <iostream>
#include <type_traits>
// Wrapper #######################################
template<class T>
struct Wrapper {};
// All you need is a way to unwrap the T, right?
template<class T>
struct Unwrap { using type = T; };
template<class T>
struct Unwrap<Wrapper<T> > : Unwrap<T> {};
// Test function #######################################
void f(int) {
  std::cout<<"int"<<std::endl;
}
// Split unwrapping and checking it with enable_if<>:
template<class T,class U=typename Unwrap<T>::type>
auto f(T) -> 
  typename std::enable_if<
    std::is_same<int,U>::value
  >::type
{
  std::cout<<"Wrapper<...<int>...>"<<std::endl;
  f(U());
}    
int main() {
  f(Wrapper<int>());  
  return 0; 
}

现场示例

您遇到的问题是由于 SFINAE 仅在模板实例化的"即时上下文"(标准使用的术语,但定义不佳)中发生。WrapperTraits<int>的实例化是在auto f<int>() -> ...实例化的直接上下文中,并且它成功了。不幸的是,WrapperTraits<int>有一个格式不正确的成员RootType。该成员的实例化不在直接上下文中,因此 SFINAE 不适用。

要使此 SFINAE 按预期工作,您需要安排WrapperTraits<int>没有名为 RootType 的类型,而不是具有这样的成员,而是具有格式不正确的定义。这就是为什么更正后的版本按预期工作的原因,尽管您可以通过重新排序来节省一些重复:

template<class T, class Enable=void>
struct WrapperTraits {};
template<class T>
struct WrapperTraits<T,typename std::enable_if<IsWrapper<T>::value>::type> {
  typedef typename GetRootType<T>::RootType RootType;
};

我可能会将整个特征系统编码为 (DEMO):

// Plain-vanilla implementation of void_t
template<class...> struct voider { using type = void; };
template<class...Ts>
using void_t = typename voider<Ts...>::type;
// WrapperTraits #######################################
// Wrapper types specialize WrappedType to expose the type they wrap;
// a type T is a wrapper type iff the type WrappedType<T>::type exists.
template<class> struct WrappedType {};
// GetRootType unwraps any and all layers of wrappers.
template<class T, class = void>
struct GetRootType {
  using type = T; // The root type of a non-WrappedType is that type itself.
};
// The root type of a WrappedType is the root type of the type that it wraps.
template<class T>
struct GetRootType<T, void_t<typename WrappedType<T>::type>> :
  GetRootType<typename WrappedType<T>::type> {};
// non-WrappedTypes have no wrapper traits.
template<class T, class = void>
struct WrapperTraits {};
// WrappedTypes have two associated types:
// * WrappedType, the type that is wrapped
// * RootType, the fully-unwrapped type inside a stack of wrappers.
template<class T>
struct WrapperTraits<T, void_t<typename WrappedType<T>::type>> {
  using WrappedType = typename ::WrappedType<T>::type;
  using RootType = typename GetRootType<T>::type;
};
// Convenience aliases for accessing WrapperTraits
template<class T>
using WrapperWrappedType = typename WrapperTraits<T>::WrappedType;
template<class T>
using WrapperRootType = typename WrapperTraits<T>::RootType;

// Some wrappers #######################################
// Wrapper<T> is a WrappedType
template<class> struct Wrapper {};
template<class T>
struct WrappedType<Wrapper<T>> {
  using type = T;
};
// A single-element array is a WrappedType
template<class T>
struct WrappedType<T[1]> {
  using type = T;
};
// A single-element tuple is a WrappedType
template<class T>
struct WrappedType<std::tuple<T>> {
  using type = T;
};

虽然那里有很多机械,而且它可能比你需要的更重。例如,可以取消WrapperTraits模板,而直接使用WrappedTypeGetRootType。我无法想象您经常需要传递WrapperTraits实例化。

相关文章: