关于不完整类型的 SFINAE 的特殊规则
Special rules regarding SFINAE for incomplete types
最近在这里回答一个问题时if-else depends on whether T is a complete type我发现下面的不编译
#include <iostream>
#include <type_traits>
using namespace std;
class Incomplete;
class Complete {};
template <typename IncompleteType>
struct DetermineCompleteHelper : public IncompleteType {};
template <typename IncompleteType, typename = std::enable_if_t<true>>
struct DetermineComplete {
static constexpr const bool value = false;
};
template <typename IncompleteType>
struct DetermineComplete<IncompleteType, std::enable_if_t<std::is_same<
decltype(DetermineCompleteHelper<IncompleteType>{}),
decltype(DetermineCompleteHelper<IncompleteType>{})>::value>> {
static constexpr const bool value = true;
};
int main() {
cout << DetermineComplete<Complete>::value << endl;
cout << DetermineComplete<Incomplete>::value << endl;
return 0;
}
但将部分模板专业化更改为
template <typename IncompleteType>
struct DetermineComplete<IncompleteType, std::enable_if_t<std::is_same<
std::integer_sequence<int, sizeof(IncompleteType)>,
std::integer_sequence<int, sizeof(IncompleteType)>>::value>> {
static constexpr const bool value = true;
};
使得代码编译无误,为什么会出现这种不规范?不应该将第一个表达式视为部分专业化上下文中的错误,从而让 SFINAE 启动并使 class 的默认定义成为实例化的定义吗?
尝试实例化 DetermineCompleteHelper<IncompleteType>
的定义时发生错误(特别是,它试图从不完整的基础 class 派生)。那不在直接上下文中。
最近在这里回答一个问题时if-else depends on whether T is a complete type我发现下面的不编译
#include <iostream>
#include <type_traits>
using namespace std;
class Incomplete;
class Complete {};
template <typename IncompleteType>
struct DetermineCompleteHelper : public IncompleteType {};
template <typename IncompleteType, typename = std::enable_if_t<true>>
struct DetermineComplete {
static constexpr const bool value = false;
};
template <typename IncompleteType>
struct DetermineComplete<IncompleteType, std::enable_if_t<std::is_same<
decltype(DetermineCompleteHelper<IncompleteType>{}),
decltype(DetermineCompleteHelper<IncompleteType>{})>::value>> {
static constexpr const bool value = true;
};
int main() {
cout << DetermineComplete<Complete>::value << endl;
cout << DetermineComplete<Incomplete>::value << endl;
return 0;
}
但将部分模板专业化更改为
template <typename IncompleteType>
struct DetermineComplete<IncompleteType, std::enable_if_t<std::is_same<
std::integer_sequence<int, sizeof(IncompleteType)>,
std::integer_sequence<int, sizeof(IncompleteType)>>::value>> {
static constexpr const bool value = true;
};
使得代码编译无误,为什么会出现这种不规范?不应该将第一个表达式视为部分专业化上下文中的错误,从而让 SFINAE 启动并使 class 的默认定义成为实例化的定义吗?
尝试实例化 DetermineCompleteHelper<IncompleteType>
的定义时发生错误(特别是,它试图从不完整的基础 class 派生)。那不在直接上下文中。