473,803 Members | 3,422 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
12 2283
>>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_arra y in the future) is pretty easy to use:
Where can I get an implementation of tr1?

Jan 13 '07 #11
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_ar ray would probably be more
convenient:

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

using std::tr1::is_ar ray;

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

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

Program Output:

true

Greg

Jan 13 '07 #12
>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_ar ray would probably be
more
convenient:

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

using std::tr1::is_ar ray;

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

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

Program Output:

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

int array[5];
bool const b = std::tr1::is_ar ray<array>::val ue;

Jan 14 '07 #13

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
5781
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
16295
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
2213
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
2224
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
3095
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
1656
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
6601
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
9565
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
10550
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
10317
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
10295
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
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5501
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
5633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.