使用隐式函数模板特化来检查标识符是否是数组 [英] Using implicit function template specialization to check if an identifier is an array

查看:57
本文介绍了使用隐式函数模板特化来检查标识符是否是数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用函数模板来验证

标识符是否为数组类型。可以使用以下模板

用于此目的:


模板< typename T,int N>

void check_array(T(&)[N])

{

}


那么使用size_t而不是int呢?

-

[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[ comp.lang.c ++。主持。第一次海报:做到这一点! ]

I''m thinking about using a function tamplate to verify that an
identifier is of an array type. May the following template be
used for this purpose:

template< typename T, int N >
void check_array( T(&)[N] )
{
}

And what about using size_t instead of int?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案

[我在clc ++中删除了交叉发布。这样文章就会更早出现]

Angel Tsankov写道:
[ I dropped cross-posting in c.l.c++.m so the articles appear sooner]

Angel Tsankov wrote:

我正在考虑使用函数模板来验证

标识符是否属于数组类型。可以使用以下模板

用于此目的:


模板< typename T,int N>

void check_array(T(&)[N])

{

}


那么使用size_t而不是int呢?
I''m thinking about using a function tamplate to verify that an
identifier is of an array type. May the following template be
used for this purpose:

template< typename T, int N >
void check_array( T(&)[N] )
{
}

And what about using size_t instead of int?



是的,我可能会使用''size_t'',虽然''int''几乎可以使用
,如果大小小于INT_MAX。


或许将它与编译时断言相结合将会更好,并且你可以尝试加入一个不错的错误

消息进入它,但应该开箱即用......


你为什么这么问?你遇到任何问题吗?


V

-

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

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

Yes, I''d probably use ''size_t'', although ''int'' would work almost
as well if the size is less than INT_MAX.

Perhaps incorporating this with a compile-time assert would be
a bit better, and you could try incorporating a decent error
message into it, but should work right out of the box...

Why do you ask? Have you run into any problem with it?

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


Angel Tsankov schrieb:
Angel Tsankov schrieb:

我正在考虑使用函数tamplate来验证

标识符是否为数组类型。可以使用以下模板

用于此目的:


模板< typename T,int N>

void check_array(T(&)[N])

{

}
I''m thinking about using a function tamplate to verify that an
identifier is of an array type. May the following template be
used for this purpose:

template< typename T, int N >
void check_array( T(&)[N] )
{
}



是的,绝对。

Yes, absolutely.


那么使用size_t而不是int呢?
And what about using size_t instead of int?



确实size_t是这种方法的规范类型,因为它是
unsigned,因为每个静态可创建的数组必须有一个

尺寸可通过sizeof表示,其本身返回size_t。

来自不来梅的问候,

DanielKrügler

-

[见 http:/ /www.gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

Indeed size_t is the canonical type for this approach, because it is
unsigned and because every statically creatable array must have a
size expressable via sizeof, which itself returns size_t.

Greetings from Bremen,

Daniel Krügler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


> [我在clc ++。m中删除了交叉发布,因此文章显示为
>[ I dropped cross-posting in c.l.c++.m so the articles appear

>更快]


Angel Tsankov写道:
>sooner]

Angel Tsankov wrote:

>我正在考虑使用一个函数tamplate以验证
标识符是否为数组类型。可以将以下模板用于此目的:

模板< typename T,int N>
void check_array(T(&)[N])
{
}

那么使用size_t而不是int呢?
>I''m thinking about using a function tamplate to verify that an
identifier is of an array type. May the following template be
used for this purpose:

template< typename T, int N >
void check_array( T(&)[N] )
{
}

And what about using size_t instead of int?



是的,我可能会使用''size_t'',虽然''int''可以使用

几乎

如果大小小于INT_MAX。


或许将其与编译时断言相结合将是

a更好,


Yes, I''d probably use ''size_t'', although ''int'' would work
almost
as well if the size is less than INT_MAX.

Perhaps incorporating this with a compile-time assert would be
a bit better,



你的想法是什么?

What is your idea?


你可以尝试加入一个体面的错误

消息进入它,但应该开箱即用...
and you could try incorporating a decent error
message into it, but should work right out of the box...



是否有标准方法来合并错误消息?

Is there a standard way to incorporate an error message?


你为什么这么问?你遇到任何问题吗?
Why do you ask? Have you run into any problem with it?



不,我没有发现任何问题。我只是好奇,如果

它是可靠的,那就是模板是否应该隐式地实例化,当且仅当参数属于数组类型时才会实例化。

No, I have not found any problems with it. I am just curious if
it is reliable, that is whether the template shall be implicitly
instantiated if and only if the argument is of an array type.


这篇关于使用隐式函数模板特化来检查标识符是否是数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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