gpt4 book ai didi

c++ - 关于不完整类型的 SFINAE 的特殊规则

转载 作者:行者123 更新时间:2023-11-27 23:53:33 25 4
gpt4 key购买 nike

最近在这里回答一个问题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 启动并使类的默认定义成为实例化的定义吗?

最佳答案

尝试实例化 DetermineCompleteHelper<IncompleteType> 的定义时发生错误(特别是,它试图从不完整的基类派生)。这超出了直接上下文。

关于c++ - 关于不完整类型的 SFINAE 的特殊规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44340209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com