使用模板检查对象是否是 class 的实例
Check if object is instance of class with template
我的class:
template < typename T >
Array<T>{};
(源数据存储在向量中)
我有一个对象:
Array< string > a;
a.add("test");
我有一个对象:
Array< Array< string > > b;
b.add(a);
如何检查:
b[0]
是 Array
的实例吗(无论模板类型如何)?
a[0]
是除 Array
之外的任何类型的实例吗?
在 C++ 中你可以使用
if(typeid(obj1)==typeid(ob2))//or typeid(obj1)==classname
cout <<"obj1 is instance of yourclassname"
在您的情况下,您可以使用 typeid(obj1)==std::array
检查
如果你可以使用 C++11,创建你的类型特征;举个例子
#include <string>
#include <vector>
#include <iostream>
#include <type_traits>
template <typename T>
struct Array
{
std::vector<T> v;
void add (T const t)
{ v.push_back(t); }
};
template <typename>
struct isArray : public std::false_type
{ };
template <typename T>
struct isArray<Array<T>> : public std::true_type
{ };
template <typename T>
constexpr bool isArrayFunc (T const &)
{ return isArray<T>::value; }
int main()
{
Array<std::string> a;
Array<Array<std::string>> b;
a.add("test");
b.add(a);
std::cout << isArrayFunc(a.v[0]) << std::endl; // print 0
std::cout << isArrayFunc(b.v[0]) << std::endl; // print 1
}
如果你不能使用C++11或更新版本而只能使用C++98,你可以简单地写成isArray
如下
template <typename>
struct isArray
{ static const bool value = false; };
template <typename T>
struct isArray< Array<T> >
{ static const bool value = true; };
并避免包含 type_traits
--- 编辑 ---
修改(在 constexpr
中转换)isArrayFunc()
,根据 Kerrek SB 的建议(谢谢!)。
下面是 提出的解决方案的较短版本,不再使用结构 isArray
.
它适用于 C++98 及更高版本。
#include <string>
#include <vector>
#include <iostream>
template <typename T>
struct Array
{
std::vector<T> v;
void add (T const t)
{ v.push_back(t); }
};
template <typename T>
constexpr bool isArrayFunc (T const &)
{ return false; }
template <typename T>
constexpr bool isArrayFunc (Array<T> const &)
{ return true; }
int main()
{
Array<std::string> a;
Array<Array<std::string>> b;
a.add("test");
b.add(a);
std::cout << isArrayFunc(a.v[0]) << std::endl; // print 0
std::cout << isArrayFunc(b.v[0]) << std::endl; // print 1
}
我的class:
template < typename T >
Array<T>{};
(源数据存储在向量中)
我有一个对象:
Array< string > a;
a.add("test");
我有一个对象:
Array< Array< string > > b;
b.add(a);
如何检查:
b[0]
是Array
的实例吗(无论模板类型如何)?a[0]
是除Array
之外的任何类型的实例吗?
在 C++ 中你可以使用
if(typeid(obj1)==typeid(ob2))//or typeid(obj1)==classname
cout <<"obj1 is instance of yourclassname"
在您的情况下,您可以使用 typeid(obj1)==std::array
检查如果你可以使用 C++11,创建你的类型特征;举个例子
#include <string>
#include <vector>
#include <iostream>
#include <type_traits>
template <typename T>
struct Array
{
std::vector<T> v;
void add (T const t)
{ v.push_back(t); }
};
template <typename>
struct isArray : public std::false_type
{ };
template <typename T>
struct isArray<Array<T>> : public std::true_type
{ };
template <typename T>
constexpr bool isArrayFunc (T const &)
{ return isArray<T>::value; }
int main()
{
Array<std::string> a;
Array<Array<std::string>> b;
a.add("test");
b.add(a);
std::cout << isArrayFunc(a.v[0]) << std::endl; // print 0
std::cout << isArrayFunc(b.v[0]) << std::endl; // print 1
}
如果你不能使用C++11或更新版本而只能使用C++98,你可以简单地写成isArray
如下
template <typename>
struct isArray
{ static const bool value = false; };
template <typename T>
struct isArray< Array<T> >
{ static const bool value = true; };
并避免包含 type_traits
--- 编辑 ---
修改(在 constexpr
中转换)isArrayFunc()
,根据 Kerrek SB 的建议(谢谢!)。
下面是 isArray
.
它适用于 C++98 及更高版本。
#include <string>
#include <vector>
#include <iostream>
template <typename T>
struct Array
{
std::vector<T> v;
void add (T const t)
{ v.push_back(t); }
};
template <typename T>
constexpr bool isArrayFunc (T const &)
{ return false; }
template <typename T>
constexpr bool isArrayFunc (Array<T> const &)
{ return true; }
int main()
{
Array<std::string> a;
Array<Array<std::string>> b;
a.add("test");
b.add(a);
std::cout << isArrayFunc(a.v[0]) << std::endl; // print 0
std::cout << isArrayFunc(b.v[0]) << std::endl; // print 1
}