首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何让SFINAE与模板专门化一起工作?

如何让SFINAE与模板专门化一起工作?
EN

Stack Overflow用户
提问于 2019-06-04 21:55:40
回答 2查看 153关注 0票数 2

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

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

代码语言:javascript
复制
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
代码语言:javascript
复制
#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>>候选方法。有可能这样做吗?

EN

回答 2

Stack Overflow用户

发布于 2019-06-05 00:03:34

看起来您想限制向量重载函数模板,使其只接受内置类型的向量。否则,这些重载就不需要SFINAE。

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

工作示例:

代码语言:javascript
复制
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);
}

输出:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2019-06-05 03:32:37

使用

代码语言:javascript
复制
template <typename T> constexpr bool isType();
template <> constexpr bool isType<int>()  {return true;}
template <> constexpr bool isType<ComplexClass>() {return false;}

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

您可以做的是对函数执行delete操作:

代码语言:javascript
复制
template <typename T> constexpr bool isType() = delete;
template <> constexpr bool isType<int>()  {return true;}
template <> constexpr bool isType<ComplexClass>() {return false;}

没有对gcc/点击的警告:Demo

所以foo(e);仍然不匹配任何重载。

我将使用标记调度来代替:

代码语言:javascript
复制
template <typename T> struct Tag{};
std::true_type isType(tag<int>);
std::false_type isType(tag<ComplexClass>);

Demo

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56445427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档