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

Good way for error generation on template instantiation

Hi @all,

I am looking for a good (compiler-independent) way to generate
meaningful error messages, if specific (unintended) templates are
instantiated.

e.g.

------------

template <typename T>
class foo
{
public:
static void someFunc() {}

};

template <>
class foo<int>
{
public:
static void someFunc() { // Compiler should throw error: No, no,
please not type "int"!!! }
}

Nov 6 '06 #1
5 1637
tt******@gmx.de wrote:
I am looking for a good (compiler-independent) way to generate
meaningful error messages, if specific (unintended) templates are
instantiated.

e.g.

------------

template <typename T>
class foo
{
public:
static void someFunc() {}

};

template <>
class foo<int>
{
public:
static void someFunc() { // Compiler should throw error: No, no,
please not type "int"!!! }
}
You could try

...
static void someFunc() {
static CREATING_OF_THIS_template_WITH_TYPE_int_PROHIBITED a;
}

which probably (hopefully) will cause the compiler complain about
undefined symbol (type) 'CREATING_OF_...'

However, the format in which error messages are given is not defined
by the Standard (which only says "diagnostic is required" in this
particular case).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 6 '06 #2
On 6 Nov 2006 06:58:24 -0800, tt******@gmx.de wrote:
>I am looking for a good (compiler-independent) way to generate
meaningful error messages, if specific (unintended) templates are
instantiated.
e.g.
------------
template <typename T>
class foo
{
public:
static void someFunc() {}

};

template <>
class foo<int>
{
public:
static void someFunc() { // Compiler should throw error: No, no,
please not type "int"!!! }
}
Templates represent a kind of macro mechanism built into the C++
language. What you try to do is against the 'spirit' of templates.
It's the user's task to decide whether a template instantiation is
appropriate or not. You shouldn't be patronizing.

Best wishes,
Roland Pibinger

Nov 6 '06 #3
>template <>
class foo<int>
{
public:
static void someFunc() { // Compiler should throw error: No, no,
please not type "int"!!! }
}
See the compile-time assert in Alexandrescu's Loki library,
which is nicely explained in in his _Modern C++ Design_.
(http://erdani.org).

Alternatively, look at Boost's BOOST_STATIC_SEARCH.

>
Templates represent a kind of macro mechanism built into the C++
language. What you try to do is against the 'spirit' of templates.
It's the user's task to decide whether a template instantiation is
appropriate or not. You shouldn't be patronizing.
Patronization and helpfulness are two different things. Almost
all templates expect their type parameters to provide certain
capabilities, but the C++ language does not currently support
type "contracts". A deliberate error message generated with a
compile-time assertion is almost always better than a
automatically generated template instantiation error. Even
worse, some templates will instantiate but yield incorrect code
for particular types whose error won't be apparent until
run-time. I prefer to catch my errors as soon as possible.

Glen Dayton
Nov 6 '06 #4

tt******@gmx.de wrote:
Hi @all,

I am looking for a good (compiler-independent) way to generate
meaningful error messages, if specific (unintended) templates are
instantiated.

e.g.

------------

template <typename T>
class foo
{
public:
static void someFunc() {}

};

template <>
class foo<int>
{
public:
static void someFunc() { // Compiler should throw error: No, no,
please not type "int"!!! }
}
Boost has a facility for this built in that probably takes into account
some compiler idiosyncracies. Using it you would do:

class foo<int>
{
BOOST_STATIC_ASSERT(false, "foo<intinstantiated");
};

keep in mind you should do this at class scope too just in case
someFunc() is never called. The compiler only instantiates, and
therefore compiles, the parts of a template that are used.

Nov 6 '06 #5

Noah Roberts wrote:
tt******@gmx.de wrote:
Hi @all,

I am looking for a good (compiler-independent) way to generate
meaningful error messages, if specific (unintended) templates are
instantiated.

e.g.

------------

template <typename T>
class foo
{
public:
static void someFunc() {}

};

template <>
class foo<int>
{
public:
static void someFunc() { // Compiler should throw error: No, no,
please not type "int"!!! }
}

Boost has a facility for this built in that probably takes into account
some compiler idiosyncracies. Using it you would do:

class foo<int>
{
BOOST_STATIC_ASSERT(false, "foo<intinstantiated");
};

keep in mind you should do this at class scope too just in case
someFunc() is never called. The compiler only instantiates, and
therefore compiles, the parts of a template that are used.
You might also consider concept_check:
http://boost.org/libs/concept_check/concept_check.htm

Instead of saying "don't instantiate with int" say exactly what the
requirements of the type are using concepts. You get a better error
message and you get code written documentation of what the template
needs.

Nov 6 '06 #6

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

Similar topics

19
by: Alf P. Steinbach | last post by:
// As usual the error message directs one to the report the bug. // // And as usual there is absolutely no way to do so without paying for // the privilege... // // Or using three or four hours...
7
by: Hunter Hou | last post by:
Hello, I'm trying one example of <<the C++ programming language>> Page 865 int f( int ); template< class T > T g( T t ) { return f( t ); } char c = g( 'a' ); ************ char f( char ); ...
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>...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
5
by: dilip ranganathan | last post by:
Hi I have taken the liberty to cross-post this. It appeared on c.l.c++.m but the ICE is regarding VS.NET 7.1 C++ compiler. post follows: ==============================================...
3
by: erictham115 | last post by:
Error C2555 c:\C++ projects\stat1\stdmatrix_adapt.h(41) : error C2555: 'std_tools::Matrix_adapter<T>::at': overriding virtual function return type differs and is not covariant from...
6
by: hsmit.home | last post by:
Hello, I came across a strange error and it's really been bugging me. Maybe someone else has come across this and any insight would be appreciated. What I'm trying to accomplish is using...
4
by: Pallav singh | last post by:
Hi All, i am getting error during explicit function Instantiation for a class Template if i do explicit Instantiation of class it work and all function symbol i get in object file But if i...
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: 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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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.