确定typename的类型? [英] Determine type of typename T?

查看:98
本文介绍了确定typename的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何确定某个特定类型名称的类型?


我正在编写模板,需要特殊的案例处理/>
特定类型:


模板< typename T>

class foo {

public:

foo(){

if(T == int)cerr<< " int\\\
";

}

};


谢谢!


Joseph


How can I determine the type of some particular typename?

I am writing a template, and it needs special case handling for some
particular types:

template <typename T>
class foo {
public:
foo() {
if (T == int) cerr << "int\n";
}
};

Thanks!

Joseph

推荐答案

Joseph Turian写道:
Joseph Turian wrote:
我如何确定某些特定的类型typename?
How can I determine the type of some particular typename?




typeid(T)


干杯

-

Mateusz ?? oskot
http://mateusz.loskot.net


在文章< 11 ********************** @ z14g2000cwz.googlegroups .com>中,

" Joseph Turian" <涂**** @ gmail.com>写道:
In article <11**********************@z14g2000cwz.googlegroups .com>,
"Joseph Turian" <tu****@gmail.com> wrote:
如何确定某个特定类型名称的类型?

我正在编写一个模板,它需要一些特殊的案例处理
特殊类型:

模板< typename T>
class foo {
public:
foo(){
if(T == int) cerr<< int \ n;
}
};
How can I determine the type of some particular typename?

I am writing a template, and it needs special case handling for some
particular types:

template <typename T>
class foo {
public:
foo() {
if (T == int) cerr << "int\n";
}
};




这是'一种可能性:


模板< typename T>

class foo {

public:

foo(){

}

};


模板<>

class foo< int> {

public:

foo(){

cerr<< int \ n;

}

};


这是'另一个:


模板< typename T>

class foo {

public:

foo();

};


模板< typename T>

foo< T> :: foo()

{

}


模板<>

foo< int> :: foo()

{

cerr<< " int\\\
";

}


这里是另一个:


#include < tr1 / type_traits>

#include< iostream>

使用std :: cerr;


template< typename T>

class foo {

public:

foo()

{

foo_imp(std :: tr1 :: is_same< T,int>());

}

private:

void foo_imp(std :: tr1 :: true_type)

{cerr<< " int\\\
英寸; }

void foo_imp(std :: tr1 :: false_type)

{}

};


-Howard



Here''s one possibility:

template <typename T>
class foo {
public:
foo() {
}
};

template <>
class foo<int> {
public:
foo() {
cerr << "int\n";
}
};

Here''s another:

template <typename T>
class foo {
public:
foo();
};

template <typename T>
foo<T>::foo()
{
}

template <>
foo<int>::foo()
{
cerr << "int\n";
}

And here''s another:

#include <tr1/type_traits>
#include <iostream>
using std::cerr;

template <typename T>
class foo {
public:
foo()
{
foo_imp(std::tr1::is_same<T, int>());
}
private:
void foo_imp(std::tr1::true_type)
{ cerr << "int\n"; }
void foo_imp(std::tr1::false_type)
{ }
};

-Howard


Mateusz Loskot写道:
Mateusz Loskot wrote:
如何确定某个特定类型名的类型? typeid(T)
How can I determine the type of some particular typename? typeid(T)




我会看看我是否可以找出使用''typeid'的语法

template< typename T>
foo< T> :: foo()
{
}

模板<>
foo< int> :: foo ()
{
cerr<< " int \ n";
}


所以foo< T> :: foo被使用,除非它被重载(由foo< int> :: foo,in

这种情况​​)。

如果我改变了声明构造函数的顺序,那么

foo< T>总是被使用,还是相同?

foo_imp(std :: tr1 :: is_same< T,int>());



I''ll see if I can figure out the syntax to use ''typeid''
template <typename T>
foo<T>::foo()
{
}

template <>
foo<int>::foo()
{
cerr << "int\n";
}
So foo<T>::foo is used unless it is overloaded (by foo<int>::foo, in
this circumstance).
If I switched the order in which the constructors were declared, would
foo<T> always be used, or would it be identical?
foo_imp(std::tr1::is_same<T, int>());




这很奇怪,我将不得不谷歌tr1。

它只适用于原始类型,还是适用于
用户定义的类?


谢谢


Joseph



This is weird, I''ll have to google tr1.
Does it only work for primitive types, or will it also work for
user-defined classes?

Thanks

Joseph


这篇关于确定typename的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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