473,378 Members | 1,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

specialized template function w/compiler error on invalid type

Hi,

Q1: Is there a way to make a template function that works only for
specific types which produces a compiler error if used with an invalid
type?
Q2: If not, how do people deal with this issue?

I believe A1 is no. I tried below with template function "f".

For A2, functors work but have ugly syntax and runtime overhead. A
dispatcher to the funtor has nice syntax but still the overhead. I'm
just guessing about the overhead, but I believe there is a ctor.

Thanks,

Chris

//==========
// f: template function defined only for char
template<typename T>
void f(T t);

template<>
void f(char i)
{
// ...
}

// g: template functor defined only for char
template<typename T>
struct g;

template<>
struct g<char>
{
void operator()(char i)
{
// ...
}
};

// h: dispatcher to g
template<typename T>
void h(T t)
{
g<T>()(t);
}

int main()
{
char c;
long l;

// f - template function w/specialization
// syntax: clean
// runtime: fast
// compiler error on invalid type: no (but linker error)
f(c);
f(l);

// g - template class w/specialization
// syntax: ugly
// runtime: slow
// compiler error on invalid type: yes
g<char>()(c);
g<long>()(l);

// h - Dispatcher function calling g
// syntax: clean
// runtime: slow
// compiler error on invalid type: yes (albeit cryptic)
h(c);
h(l);

return 0;
}

Nov 9 '06 #1
5 2335
* ch*************@yahoo.com:
Hi,

Q1: Is there a way to make a template function that works only for
specific types which produces a compiler error if used with an invalid
type?
Yes. The general technique is to use SFINAE. The Boost library
provides ready-to-use helper classes for this.

Q2: If not, how do people deal with this issue?
N/A.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 9 '06 #2
ch*************@yahoo.com wrote:
Q1: Is there a way to make a template function that works only for
specific types which produces a compiler error if used with an invalid
type?
I am sorry, but this is just silly. Why do you need it to be a template
then? Drop the type from the template, replace it with your specific
type (for which you need it work) and be happy.
Q2: If not, how do people deal with this issue?
Normal people don't. See above.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 9 '06 #3

Victor Bazarov wrote:
Why do you need it to be a template
I use the template facility to use a specialized implementation for 64
bit long. Overloading cannot do this AFAIK.

Nov 9 '06 #4
ch*************@yahoo.com wrote:
Victor Bazarov wrote:
>Why do you need it to be a template

I use the template facility to use a specialized implementation for 64
bit long. Overloading cannot do this AFAIK.
Just disregard my message; I failed to notice plural in your "types"
requirement. Sorry. I've already cancelled it...

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

Alf P. Steinbach wrote:
use SFINAE.
Wow, thanks. Google search showed me some amazing stuff.

Chris

Nov 10 '06 #6

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

Similar topics

1
by: Martin Magnusson | last post by:
I have a partially specialized templated class, which at one point needs to pass pointers to some of its methods to another function. However, it seems that my compiler (gcc) and me don't quite...
2
by: Hartmut Sbosny | last post by:
Hello NG, I have a question. I have a header file with a function template and a fully specialized version of it, for instance //======== File "templ.hpp" ======== #include <iostream> // the...
3
by: Tomás | last post by:
First of all, the following will compile for me without any problems: class BearerOfType { public: class SpecialType {}; };
2
by: Greg Buchholz | last post by:
/* I've been experimenting with some generic/polytypic programs, and I've stumbled on to a problem that I can't quite figure out. In the program below, I'm trying to define a generic version of...
4
by: Damien | last post by:
Hi all, I've run into something confusing on MS VC6. Yeah I know it's old but that's what the client wants, so... I'm trying to pass a pointer to a member function as a template argument,...
8
by: Jess | last post by:
Hi, I have a template function that triggered some compiler error. The abridged version of the class and function is: #include<memory> using namespace std; template <class T>
2
by: Aarti | last post by:
Say I have a class template as follows template<typename T> class foo { public: void test(); }; template<typename T>
9
by: rtalbot | last post by:
I've got a container that looks like this: template <class T> class Foo { public: Foo() : _data(), _status(1) { } Foo(T) : _data(T), _status(0) { } ~Foo() { }
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.