By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

Good way for error generation on template instantiation

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
>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

P: n/a

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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.