473,385 Members | 1,593 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,385 software developers and data experts.

Forbidding template instantiation for specific type

Hi,

I am modernizing a small library that is being currently in use by some
people. In the new version I want to forbid use of a template function with
argument of const char * (i.e. I want to get compile errors when it
happens). For the rest of the types function should work ok.

// A template function, in old version of the library allowed for all types
template<typename T> void func(T t)
{
/* ... */
}

Now I want to forbid its use with const char *. For example I could use
specialization in the following way:

template<> void func(const char *)
{
const int a = 0;
a = 1;
}

Is there a cleaner way of achieving the same? Expecially to get better error
message? (ideally the error message should say that func(const char*) is
undefined).

Best regards,
Marcin

Jul 22 '05 #1
5 2107
Marcin Kalicinski wrote:
Hi,

I am modernizing a small library that is being currently in use by some
people. In the new version I want to forbid use of a template function with
argument of const char * (i.e. I want to get compile errors when it
happens). For the rest of the types function should work ok.

// A template function, in old version of the library allowed for all types
template<typename T> void func(T t)
{
/* ... */
}

Now I want to forbid its use with const char *. For example I could use
specialization in the following way:

template<> void func(const char *)
{
const int a = 0;
a = 1;
}

Is there a cleaner way of achieving the same? Expecially to get better error
message? (ideally the error message should say that func(const char*) is
undefined).


Error messages vary across implementations, but you can try

template <> void func (const char *)
{
int constraints_func_undefined_for_pointer_to_const_ch ar [-1];
}

Regards,
Buster.
Jul 22 '05 #2
Marcin Kalicinski wrote:
Hi,

I am modernizing a small library that is being currently in use by some
people. In the new version I want to forbid use of a template function with
argument of const char * (i.e. I want to get compile errors when it
happens). For the rest of the types function should work ok.

// A template function, in old version of the library allowed for all types
template<typename T> void func(T t)
{
/* ... */
}

Now I want to forbid its use with const char *. For example I could use
specialization in the following way:

template<> void func(const char *)
{
const int a = 0;
a = 1;
}

Is there a cleaner way of achieving the same? Expecially to get better error
message? (ideally the error message should say that func(const char*) is
undefined).


Oh man. That's too funny. Ignore my other post. Just don't define it!

template <> void func (const char *);

You'll get a link error.

Regards,
Buster.
Jul 22 '05 #3
Uzytkownik "Buster" <no***@nowhere.com> napisal w wiadomosci
news:c4**********@newsg3.svr.pol.co.uk...
Marcin Kalicinski wrote:
Hi,

I am modernizing a small library that is being currently in use by some
people. In the new version I want to forbid use of a template function with argument of const char * (i.e. I want to get compile errors when it
happens). For the rest of the types function should work ok.

// A template function, in old version of the library allowed for all types template<typename T> void func(T t)
{
/* ... */
}

Now I want to forbid its use with const char *. For example I could use
specialization in the following way:

template<> void func(const char *)
{
const int a = 0;
a = 1;
}

Is there a cleaner way of achieving the same? Expecially to get better error message? (ideally the error message should say that func(const char*) is
undefined).


Oh man. That's too funny. Ignore my other post. Just don't define it!

template <> void func (const char *);

You'll get a link error.


Now that's a clever idea. Thanks!

Best regards,
Marcin
Jul 22 '05 #4


Oh man. That's too funny. Ignore my other post. Just don't define it!

template <> void func (const char *);

You'll get a link error.


Now that's a clever idea. Thanks!

Best regards,


I guess I don't see how that is an improvement over the OP's initial method.
Both will confuse an unsuspecting user of the template who isn't "In the
know". I other words, if I try to use that template with a const char *,
and I get a link error, I'm gonna be one confused dude, and can't imagine
coming to the conclusion that it is intended behaviour! I actually liked the
first idea of creating an array with an informative name, and giving it a
negative number as a size. I actually found that to be pretty clever! The
trick is giving the array a name that makes the situation obvious, like
"USER_this_template_does_not_work_with_const_char_ pointers"
Jul 22 '05 #5
Dan Moos wrote:
Oh man. That's too funny. Ignore my other post. Just don't define it!

template <> void func (const char *);

You'll get a link error.


Now that's a clever idea. Thanks!

Best regards,

I guess I don't see how that is an improvement over the OP's initial method.
Both will confuse an unsuspecting user of the template who isn't "In the
know". I other words, if I try to use that template with a const char *,
and I get a link error, I'm gonna be one confused dude, and can't imagine
coming to the conclusion that it is intended behaviour! I actually liked the
first idea of creating an array with an informative name, and giving it a
negative number as a size. I actually found that to be pretty clever! The
trick is giving the array a name that makes the situation obvious, like
"USER_this_template_does_not_work_with_const_char_ pointers"


Good point. We should use plain English. I was about to suggest
'function_func_is_not_implemented_for_arguments_of _type_pointer_to_const_char'
before I thought of the other idea. But the OP did ask for an error
saying that the function was undefined.
Jul 22 '05 #6

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

Similar topics

7
by: zhou | last post by:
Hi there, We have a compiler specific issue which requires us to force template instantiation. This works fine. The problem comes when I try using std:find() on vector. Since vector has no member...
5
by: Marcin Kalicinski | last post by:
Hi, I am modernizing a small library that is being currently in use by some people. In the new version I want to forbid use of a template function with argument of const char * (i.e. I want to...
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
10
by: Suki | last post by:
Hi, I'm writing a templated class, and i dont want to use the class otherthan for some predetermined types, say, int, double etc. This class has no meaning for typenames other than those few. ...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
5
by: Sunny | last post by:
Hi, I want to create a library out of a C++ sources. The implementation should be in object files and there will be a header file which the client will #include. I want to use templates to...
2
by: aitrob | last post by:
Hi, I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if...
11
by: Juha Nieminen | last post by:
I'm writing an STL-style data container, and this problem is puzzling me. The following code should demonstrate the problem: //---------------------------------------------------------- #include...
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
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.