gpt4 book ai didi

c++ - 如何将 SFINAE 与 nullary 成员函数一起使用?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:11:17 26 4
gpt4 key购买 nike

<分区>

我有一个带有 bool 模板参数 can_fly 的类模板 Bird。根据该值,我想启用一个带有签名 void fly(); 的成员函数。

这是我的代码:

#include <type_traits>

template<bool can_fly>
class Bird {
public:
template<typename void_t = typename std::enable_if<can_fly>::type>
void_t fly() { /* ... */ }
};

int main() {
Bird<true> flyingBird;
flyingBird.fly();
Bird<false> flightlessBird;

return 0;
}

此代码在 Visual Studio 2015 中编译良好,但 GCC 提示在 main 的第三行中“‘struct std::enable_if’中没有名为‘type’的类型

我认为在 false 情况下没有 ::type 的事实是 SFINAE 的全部要点。有人可以向我解释我做错了什么以及正确的方法是什么吗?

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