gpt4 book ai didi

c++ - 为什么这种替换失败会再次产生错误?

转载 作者:行者123 更新时间:2023-12-04 03:25:39 25 4
gpt4 key购买 nike

<分区>

我刚才问了一个关于为什么 std::enable_if<false> 的问题不能在 SFINAE 上下文中使用,如:

template <typename T, typename DEFAULTVOID = void>
struct TemplatedStruct {};

template <typename T>
struct TemplatedStruct<T, std::enable_if_t<false>> {}; // enable_if expression
// isn't dependent on template type, is always false and so is an error

但是在下面的示例中它依赖于模板参数,但这也会产生错误:

#include <type_traits>

template <typename value_t_arg>
struct underlyingtype
{
static inline constexpr bool bIsIntegralType =
std::is_integral_v<value_t_arg>;

template <typename T, typename DEFAULTVOID = void>
struct IsSpecialType {
static inline constexpr bool bIsSpecialType = false;
};

template <typename T>
struct IsSpecialType<T, std::enable_if_t<bIsIntegralType>> {
static inline constexpr bool bIsSpecialType = true;
};

// This also creates an error, this is essentially the same as above
template <typename T>
struct IsSpecialType<T, std::enable_if_t<std::is_integral_v<value_t_arg>>> {
static inline constexpr bool bIsSpecialType = true;
};


};

int main()
{
underlyingtype<int> g1; // Works
underlyingtype<double> g2; // std::enable_if_t<false, void>:
// Failed to specialize alias template
}

在第一种情况下使用std::enable_if_t<false>无论我实例化什么,它都无法编译。然而在另一种情况下underlyingtype<int> g1;工作,而当我用双实例化它然后无法编译时,这让我认为它们是两个不同的问题。

编辑:我应该提一下,这无法使用 Visual Studio Community 2019 16.9.3 进行编译。

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