gpt4 book ai didi

c++ - 如何使 SFINAE 使用模板特化?

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

我有一个模板方法 foo .我想要几个不同的实现:对于 T , vector<T>vector<vector<T>>其中 T是内置类型或一些复杂的类。我想使用 SFINAE 来分离内置类型和类的实现,并限制一组允许的类型。

以下代码工作正常,但我收到警告消息:

8:37: warning: inline function 'constexpr bool isType() [with T =
std::vector<int>]' used but never defined

8:37: warning: inline function 'constexpr bool isType() [with T =
std::vector<std::vector<int> >]' used but never defined
#include <type_traits>
#include <vector>

using namespace std;

class ComplexClass{};

template<typename T> constexpr bool isType();
template<> constexpr bool isType<int>() {return true;}
template<> constexpr bool isType<ComplexClass>() {return false;}

template <typename T>
inline typename enable_if<isType<T>(), void>::type
foo(T& value) {}

template <typename T>
inline typename enable_if<!isType<T>(), void>::type
foo(T& value) {}

template <typename T>
inline typename enable_if<isType<T>(), void>::type
foo(vector<T>& value) {}

template <typename T>
inline typename enable_if<isType<T>(), void>::type
foo(vector<vector<T>>& value) {}

int main()
{
int a;
vector<int> b;
vector<vector<int>> c;
ComplexClass d;
char e;
foo(a);
foo(b);
foo(c);
foo(d);
// foo(e); // has to lead to an error
return 0;
}

看起来编译器试图通过 vector<...>进入第一个enable_if方法并失败。但是跳过这些方法会很好,因为我们有更好的候选者 vector<T>vector<vector<T>> .可以吗?

最佳答案

您似乎想将 vector 重载函数模板限制为仅接受内置类型的 vector 。否则这些重载不需要 SFINAE。

您还可以使用 std::is_fundamental检测内置类型:

工作示例:

using namespace std;

class ComplexClass {};

template <typename T>
typename enable_if<is_fundamental<T>::value>::type
foo(T& value) { cout << __PRETTY_FUNCTION__ << '\n'; }

template <typename T>
typename enable_if<!is_fundamental<T>::value>::type
foo(T& value) { cout << __PRETTY_FUNCTION__ << '\n'; }

template <typename T>
typename enable_if<is_fundamental<T>::value>::type
foo(vector<T>& value) { cout << __PRETTY_FUNCTION__ << '\n'; }

template <typename T>
typename enable_if<is_fundamental<T>::value>::type
foo(vector<vector<T>>& value) { cout << __PRETTY_FUNCTION__ << '\n'; }

int main() {
int a;
vector<int> b;
vector<vector<int>> c;
ComplexClass d;
char e;
foo(a);
foo(b);
foo(c);
foo(d);
foo(e);
}

输出:

typename std::enable_if<std::is_fundamental<_Tp>::value>::type foo(T&) [with T = int; typename std::enable_if<std::is_fundamental<_Tp>::value>::type = void]
typename std::enable_if<std::is_fundamental<_Tp>::value>::type foo(std::vector<_Tp>&) [with T = int; typename std::enable_if<std::is_fundamental<_Tp>::value>::type = void]
typename std::enable_if<std::is_fundamental<_Tp>::value>::type foo(std::vector<std::vector<_Tp> >&) [with T = int; typename std::enable_if<std::is_fundamental<_Tp>::value>::type = void]
typename std::enable_if<(! std::is_fundamental<_Tp>::value)>::type foo(T&) [with T = ComplexClass; typename std::enable_if<(! std::is_fundamental<_Tp>::value)>::type = void]
typename std::enable_if<std::is_fundamental<_Tp>::value>::type foo(T&) [with T = char; typename std::enable_if<std::is_fundamental<_Tp>::value>::type = void]

关于c++ - 如何使 SFINAE 使用模板特化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56445427/

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