如何使SFINAE与模板专业化一起工作?

时间:2019-06-04 13:55:40

标签: c++ c++11 templates sfinae enable-if

我有一个模板方法foo。我想要几种不同的实现:Tvector<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>>候选者。有可能吗?

2 个答案:

答案 0 :(得分:1)

您似乎希望将向量重载函数模板限制为仅接受内置类型的向量。否则,这些重载不需要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]

答案 1 :(得分:0)

使用

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

isType<char>存在,即使缺少定义。

您可以做的是delete函数:

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

没有gcc / clang警告:Demo

所以foo(e);仍然没有任何重载。

我会改用标签分配:

template <typename T> struct Tag{};
std::true_type isType(tag<int>);
std::false_type isType(tag<ComplexClass>);

Demo