Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using implicit function template specialization to check if an identifier is an array

Reply
Thread Tools

Using implicit function template specialization to check if an identifier is an array

 
 
Angel Tsankov
Guest
Posts: n/a
 
      01-12-2007
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! ]

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-12-2007
[ I dropped cross-posting in c.l.c++.m so the articles appear sooner]

Angel Tsankov wrote:
> 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?


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


 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Daniel_Kr=FCgler?=
Guest
Posts: n/a
 
      01-12-2007
Angel Tsankov schrieb:

> 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.


> And what about using size_t instead of int?


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! ]

 
Reply With Quote
 
Angel Tsankov
Guest
Posts: n/a
 
      01-12-2007
>[ I dropped cross-posting in c.l.c++.m so the articles appear
>sooner]
>
> Angel Tsankov wrote:
>> 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?

>
> 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.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-12-2007
Angel Tsankov wrote:
>> [ I dropped cross-posting in c.l.c++.m so the articles appear
>> sooner]
>>
>> Angel Tsankov wrote:
>>> 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?

>>
>> 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?


Check out Boost's compile-time assertions based on templates.
They have a whole bunch of them, IIRC.

>> 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?


No. And it's not a simple task either. If you call your function
'check_array', and it fails, what error message do you see? Would
naming 'check_array' differently clarify it? You could try using
SFINAE mechanism to even cause something like "not an array" from
your particular compiler, I bet.

>> 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.


Yes, that's certaintly so.

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


 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      01-12-2007
Angel Tsankov wrote:
> 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] )
> {
> }


The only change I would consider is making it so it can be used statically.

e.g.

template< typename T, unsigned N >
char (check_array_helper( T(&)[N] ))[N];

#define check_array(X) (sizeof check_array_helper(X) )


Perhaps you could eliminate the marco using somthing like this.

template< typename T>
struct check_array;

template< typename T, unsigned N >
struct check_array< T(&)[N] >
{
static const bool is_array = true;
};


Now you can use somthing like this:
check_array<var>::is_array



>
> And what about using size_t instead of int?


Use size_t.

 
Reply With Quote
 
Angel Tsankov
Guest
Posts: n/a
 
      01-13-2007
>> 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] )
>> {
>> }

>
> The only change I would consider is making it so it can be used
> statically.
>
> e.g.
>
> template< typename T, unsigned N >
> char (check_array_helper( T(&)[N] ))[N];
>
> #define check_array(X) (sizeof check_array_helper(X) )
>
>
> Perhaps you could eliminate the marco using somthing like this.
>
> template< typename T>
> struct check_array;
>
> template< typename T, unsigned N >
> struct check_array< T(&)[N] >
> {
> static const bool is_array = true;
> };
>
>
> Now you can use somthing like this:
> check_array<var>::is_array


Well, one may only specify a typename or an integer as a template
argument. In fact, this is the basic difference between class
templates and function templates - function template parameters
may be deduced from the arguments to the function. Class template
parameters however, may not be deduced.

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      01-13-2007
Angel Tsankov wrote:
> 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] )
> {
> }


How do you envision using the check_array() function template to a test
for an array type exactly? If anything, std::tr1::is_array (or
std::is_array in the future) is pretty easy to use:

#include <tr1/type_traits>

char a[30];

using std::tr1::is_array;

static bool b = is_array<a>::value;

Greg

 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      01-13-2007
Angel Tsankov wrote:
....
>> Now you can use somthing like this:
>> check_array<var>::is_array

>
> Well, one may only specify a typename or an integer as a template
> argument. In fact, this is the basic difference between class templates
> and function templates - function template parameters may be deduced
> from the arguments to the function. Class template parameters however,
> may not be deduced.


Yep - I don't know what I was thinking...
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      01-13-2007
Greg wrote:
> Angel Tsankov wrote:
>> 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] )
>> {
>> }

>
> How do you envision using the check_array() function template to a test
> for an array type exactly? If anything, std::tr1::is_array (or
> std::is_array in the future) is pretty easy to use:
>
> #include <tr1/type_traits>
>
> char a[30];
>
> using std::tr1::is_array;
>
> static bool b = is_array<a>::value;
>
> Greg
>


Only works for variables with external linkage.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I fix the warning C4786 'identifier' : identifier wastruncated to 'number' characters in the debug information...? babakandme@yahoo.com C++ 2 12-11-2007 08:17 PM
identifier not found error, undeclared identifier aarthi28@gmail.com C++ 2 02-26-2007 02:11 AM
template specialization overriding non-specialization? Joseph Turian C++ 2 04-16-2006 02:46 PM
template, implicit specialization? C++ 1 09-18-2005 04:28 AM
Trying to declare identifier locally and hide a file scope identifier Kobu C Programming 7 02-25-2005 05:19 AM



Advertisments