类模板中的 typedef 的 SFINAE 失败是指另一个类模板中的 typedef
SFINAE failure with typedef in class template referring to typedef in another class template
我一直在研究一种方法,以生成有关将其他类包装在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> {
};
但是,如果有人能看到更优雅的f
和WrapperTraits
书写方式,我会非常有兴趣看到它!
你问题的第一部分是为什么它失败了。答案是,在编译时级别,&&
没有短路属性。这一行:
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
模板,而直接使用WrappedType
和GetRootType
。我无法想象您经常需要传递WrapperTraits
实例化。
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 运行同一解决方案的另一个项目的项目
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- C++从另一个类访问公共静态向量的正确方法是什么
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 使用std::transform将一个范围的元素添加到另一个范围中
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 修改函数中的指针(将另一个指针作为参数传递)
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- C++试图读取一个文件并输出到另一个文本文件
- 如何将指针从一个void函数传递到另一个C++
- 如何从另一个文件继承私有成员变量和公共函数
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 将一个 typedef 函数指向另一个类中的另一个函数
- 为什么当一个模板类继承自另一个模板类时,需要重新指定 typedef 并限定函数调用?
- 课堂上的Typedef.在另一个类错误中:尚未声明类型
- C++ 使用另一个类的 typedef'ed 成员时出现编译错误
- 类模板中的 typedef 的 SFINAE 失败是指另一个类模板中的 typedef