将模板类限制为某些类型? [英] limiting a template class to certain types ?

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

问题描述

是否可以将模板类限制为仅限某些类型。我在网上发现了一些东西,但是在编译时似乎没有什么东西适用。


模板< typename T>

class AClass

{

public:

AClass(){}

};


AClass< floatfloatSomethng; //是的,没关系

AClass< intintSomething; //不,我们不想创造

那个对象


谢谢你,-mark

Is it possible to limit a template class to certain types only. I found
a few things on the net but nothing seems to apply at compile time.

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

AClass<floatfloatSomethng; // yes it''s fine
AClass<intintSomething; // no we don''t want to allow the creation of
that object

Thank you, -mark

推荐答案

ma*****@yahoo.com 写道:

是否可以将模板类限制为仅限某些类型。我在网上发现了一些东西,但是在编译时似乎没有什么东西适用。


模板< typename T>

class AClass

{

public:

AClass(){}

};


AClass< floatfloatSomethng; //是的,没关系

AClass< intintSomething; //不,我们不想允许创建

该对象
Is it possible to limit a template class to certain types only. I found
a few things on the net but nothing seems to apply at compile time.

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

AClass<floatfloatSomethng; // yes it''s fine
AClass<intintSomething; // no we don''t want to allow the creation of
that object



模板< typename T>

class AClass {

public:

AClass(){}

};


模板< class AClass< int> ;;


int main()

{

AClass< doublead;

AClass< intai; //错误

}

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

template<class AClass<int>;

int main()
{
AClass<doublead;
AClass<intai; // error
}


ma*****@yahoo.com 写道:

是否可以将模板类限制为仅限某些类型。我在网上发现了一些东西但是在编译时似乎没有什么东西适用于

时间。


模板< typename T> ;

class AClass

{

public:

AClass(){}

};


AClass< floatfloatSomethng; //是的,没关系

AClass< intintSomething; //不,我们不想允许创建

对象
Is it possible to limit a template class to certain types only. I
found a few things on the net but nothing seems to apply at compile
time.

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

AClass<floatfloatSomethng; // yes it''s fine
AClass<intintSomething; // no we don''t want to allow the creation of
that object



最简单的方法可能是隐藏你的实现模板

键入一个单独的翻译单元并使用显式实例化

将相同的TU与类的实现填充为某些

特定类型。一个更好的方法是设计它,所以没有

需要限制它。


或者你可以做这样的事情(AKA类型特征 ;):


模板<类Tclass允许{enum {yes};然后
//然后将它专门用于你允许的类型:


模板<类允许< float {public:enum {yes = 1}; };

模板<类允许< double {public:enum {yes = 1}; };


//最后,使用它:


模板< typename T>

class AClass

{

static const bool allowed = allowed< T> :: yes;


public:

AClass(){}

};


int main(){

AClass< floataf;

AClass< intaf; //错误

}


V

-

请删除大写''A'在通过电子邮件回复时

我没有回复最热门的回复,请不要问

The simplest way is probably hide the implementation of your template
type in a separate translation unit and use explicit instantiations
to stuff the same TU with the implementations of the class for some
specific types. A better way would be to design it so there is no
need to limit it.

Or you could do something like this (AKA "type traits"):

template<class Tclass allowed { enum { yes }; };

// then specialise it for the types that you allow:

template<class allowed<float{ public: enum { yes = 1 }; };
template<class allowed<double{ public: enum { yes = 1 }; };

// and last, use it:

template<typename T>
class AClass
{
static const bool allowed = allowed<T>::yes;

public:
AClass() {}
};

int main() {
AClass<floataf;
AClass<intaf; // error
}

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


ma ***** @ yahoo.com 发布:

ma*****@yahoo.com posted:


AClass< floatfloatSomethng; //是的,没关系

AClass< intintSomething; //不,我们不想允许创建
AClass<floatfloatSomethng; // yes it''s fine
AClass<intintSomething; // no we don''t want to allow the creation of



此页面上的示例可能会让您感兴趣:

http://www.boost.org/doc/html/boost_staticassert.html


(然而,其中一个例子是可疑的,它应该使用

" numeric_limits< T> :: digits而不是" sizeof(T)* CHAR_BIT")。


-


Frederick Gotham


The examples on this page may interest you:

http://www.boost.org/doc/html/boost_staticassert.html

(However, one of the examples is dubious in that it should use
"numeric_limits<T>::digits" rather than "sizeof(T) * CHAR_BIT" ).

--

Frederick Gotham


这篇关于将模板类限制为某些类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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