473,789 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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++.m oderated. First time posters: Do this! ]

Jan 12 '07 #1
12 2282
[ 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
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++.m oderated. First time posters: Do this! ]

Jan 12 '07 #3
>[ 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
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
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_he lper( T(&)[N] ))[N];

#define check_array(X) (sizeof check_array_hel per(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
>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_he lper( T(&)[N] ))[N];

#define check_array(X) (sizeof check_array_hel per(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
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_ar ray (or
std::is_array in the future) is pretty easy to use:

#include <tr1/type_traits>

char a[30];

using std::tr1::is_ar ray;

static bool b = is_array<a>::va lue;

Greg

Jan 13 '07 #8
Angel Tsankov wrote:
....
>Now you can use somthing like this:
check_array<va r>::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
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_ar ray (or
std::is_array in the future) is pretty easy to use:

#include <tr1/type_traits>

char a[30];

using std::tr1::is_ar ray;

static bool b = is_array<a>::va lue;

Greg
Only works for variables with external linkage.
Jan 13 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6490
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. Note that the overload is identical to the specialization except, of course, for the missing "template <>". I don't know if my questions will be a bit too broad or not, but I thought I'd give it shot... When is overloading preferable to...
7
5076
by: CoolPint | last post by:
While I was testing my understanding of Functioin Template features by playing with simple function templates, I got into a problem which I cannot understand. I would be very grateful if someone offered me a kind explanation. TIA Function template and specialization below works fine: template <typename T> T maximum (T a, T b) {
2
5780
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem which I can't figure out. I have a simple class C with 2 template member objects, and a function print() that prints the value of these objects. I defined a specialization of print() for C<string, char> which is called correctly. Then I wanted...
16
16292
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
1
2212
by: | last post by:
Hi everyone, I've got this pice of code: template <bool cond, typename A, typename B> struct Select{ typedef A Result; }; template <typename A, typename B> struct Select<false, A, B>{ typedef B Result;
1
2223
by: Kai-Uwe Bux | last post by:
Hi folks, I would like to know which clause of the standard rules out this: template < typename eval > struct recursive_template { typedef typename eval::enum_type Enum;
8
3094
by: Oskar Enoksson | last post by:
The following template specialization construct is rejected by g++ 4.1.2 while icc 9.1 silently accepts is (even with -strict-ansi). Which compiler is correct? template<typename T> struct x {}; template<typename T, unsigned N>
5
1653
by: desktop | last post by:
I have this example: template<class T(1) void f( T ); template<class T(2) void f( T* ); template< (3)
13
6599
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm trying to use from an executable, compile using gcc 4.1.2. Everything works fine until I try specializing one of the static member function templates in a non-template class. I have a feeling I'm messing up something obvious so before I post a...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10410
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.