gpt4 book ai didi

c++ - 成员函数 SFINAE 错误 C2938

转载 作者:太空狗 更新时间:2023-10-29 20:12:23 25 4
gpt4 key购买 nike

此代码在 VS2013 中编译失败。

template<typename T>
class SomeClass {
public:
std::enable_if_t<std::is_fundamental<T>::value, T>
DoSomething() {
return T();
}

std::enable_if_t<!std::is_fundamental<T>::value, T>
DoSomething() {
return T();
}
};

如何在VS2013中实现(DoSomething必须是非静态成员函数)?

最佳答案

SFINAE 仅适用于模板。您的 DoSomething() 不是模板,它是类模板的非模板成员。您需要将其设为模板:

template<typename T>
class SomeClass {
public:
template <class U = T>
std::enable_if_t<std::is_fundamental<U>::value, T>
DoSomething() {
return T();
}

template <class U = T>
std::enable_if_t<!std::is_fundamental<U>::value, T>
DoSomething() {
return T();
}
};

[Live example]

关于c++ - 成员函数 SFINAE 错误 C2938,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28043532/

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