473,758 Members | 2,340 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1660
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_THI S_template_WITH _TYPE_int_PROHI BITED 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_SE ARCH.

>
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_AS SERT(false, "foo<intinstant iated");
};

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_AS SERT(false, "foo<intinstant iated");
};

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
3554
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 to find the _current_ reporting page... #include <vector> #include <iostream>
7
1710
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 ); Stroustrup said **** line is wrong because alternative resolution of f( t )
7
2479
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> registrar; }; The constructor of Registrar does the registering when it is initialized.
72
5890
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 example, between a C/C++ programmers with a few weeks of experience and a C/C++ programmer with years of experience. You don't really need to understand the subtle details or use the obscure features of either language
2
2019
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. This makes it difficult to spot errors. For example, F4 to "jump to next error" just jumps ot the next "template assistance" message. And since there are hundreds of them, this is quite obnoxious. Can I disable these things? Why is MSVC...
5
1674
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: ============================================== John Torjo wrote: >
3
2961
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 'ple::imtx_impl<T>::at' //in my program: the derived class template <class T> class Matrix_adapter : public ple::imtx_impl<T{ protected:
6
2547
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 boost::lexical_cast to cast a vector of strings to a string. The compiler I'm using is MSVC++ 2005 Express Edition. The gc++
4
1599
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 try to expose only one function of my class its failing #include <iostream>
1
9885
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
9740
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8744
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
7287
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
6564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5175
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
5332
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3402
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2702
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.