如何查询if(T == int)与模板类 [英] how to query if(T==int) with template class

查看:106
本文介绍了如何查询if(T == int)与模板类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在模板类中写一个函数时,如何找到我的T是什么?



例如

  template< typename T> 
ostream&运算符<< (ostream& out,Vector< T& vec)
{
if(typename T == int)
}
解决方案

如果我写上面的if语句, p>这样的:

 模板<类T> 
struct TypeIsInt
{
static const bool value = false;
};

模板<>
struct TypeIsInt< int>
{
static const bool value = true;
};

template< typename T>
ostream&运算符<< (ostream& out,Vector< T>& vec)
{
if(TypeIsInt< T> value)
// ...
}


When I'm writing a function in a template class how can I find out what my T is?

e.g.

template <typename T>
ostream& operator << (ostream &out,Vector<T>& vec)
{
if (typename T == int)
}

How can I write the above if statement so it works?

解决方案

Something like this:

template< class T >
struct TypeIsInt
{
    static const bool value = false;
};

template<>
struct TypeIsInt< int >
{
    static const bool value = true;
};

template <typename T>
ostream& operator << (ostream &out,Vector<T>& vec)
{
    if (TypeIsInt< T >::value)
    // ...
}

这篇关于如何查询if(T == int)与模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆