By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,433 Members | 776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,433 IT Pros & Developers. It's quick & easy.

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

P: n/a
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! ]

Jan 12 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
[ 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
Jan 12 '07 #2

P: n/a
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! ]

Jan 12 '07 #3

P: n/a
>[ 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.

Jan 12 '07 #4

P: n/a
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
Jan 12 '07 #5

P: n/a
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.

Jan 12 '07 #6

P: n/a
>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.

Jan 13 '07 #7

P: n/a
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

Jan 13 '07 #8

P: n/a
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...
Jan 13 '07 #9

P: n/a
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.
Jan 13 '07 #10

P: n/a
>>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:
Where can I get an implementation of tr1?

Jan 13 '07 #11

P: n/a
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 test for
an array type? I think std::tr1::is_array would probably be more
convenient:

#include <iostream>
#include <ios>
#include <tr1/type_traits>

using std::tr1::is_array;

int main()
{
bool b = is_array< int[5] >::value;

std::cout << std::boolalpha << b << "\n";
}

Program Output:

true

Greg

Jan 13 '07 #12

P: n/a
>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
test for
an array type? I think std::tr1::is_array would probably be
more
convenient:

#include <iostream>
#include <ios>
#include <tr1/type_traits>

using std::tr1::is_array;

int main()
{
bool b = is_array< int[5] >::value;

std::cout << std::boolalpha << b << "\n";
}

Program Output:

true
Can one use std::tr1::is_array with array, e.g.:

int array[5];
bool const b = std::tr1::is_array<array>::value;

Jan 14 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.