Good way for error generation on template instantiation

T

tthunder

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"!!! }
}
 
V

Victor Bazarov

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
 
R

Roland Pibinger

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
 
G

Glen Dayton

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
 
N

Noah Roberts

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<int> instantiated");
};

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.
 
N

Noah Roberts

Noah said:
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<int> instantiated");
};

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top