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

Any way to force compile failure?

P: n/a

Is there any way to force compile failure if a particular compile-time
constant doesn't evaluate to true?

Consider a template which was intended to only work with unsigned
integers (whether they be char, short, int, long):
#include <limits>

template<class T>
void Func()
{
CompileErrorIfTrue( std::numeric_limits<T>::is_signed );

/* Rest of function */
}
I suppose we could do something like:
template<bool condition>
void CompileErrorIfTrue();
/* Definition missing in order to trigger compile error */

template<>
void CompileErrorIfTrue<false>() {}

#include <limits>

template<class T>
void Func()
{
CompileErrorIfTrue<std::numeric_limits<T>::is_sign ed>();

/* Rest of function */
}

int main()
{
Func<int>();
}
--

Frederick Gotham
Jun 27 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Frederick Gotham wrote:
Is there any way to force compile failure if a particular compile-time
constant doesn't evaluate to true?

Consider a template which was intended to only work with unsigned
integers (whether they be char, short, int, long):
#include <limits>

template<class T>
void Func()
{
CompileErrorIfTrue( std::numeric_limits<T>::is_signed );

/* Rest of function */
}
I suppose we could do something like:
template<bool condition>
void CompileErrorIfTrue();
/* Definition missing in order to trigger compile error */

template<>
void CompileErrorIfTrue<false>() {}

#include <limits>

template<class T>
void Func()
{
CompileErrorIfTrue<std::numeric_limits<T>::is_sign ed>();

/* Rest of function */
}

int main()
{
Func<int>();
}
--

Frederick Gotham


Yes. Boost and Loki each have a facility for this. See for instance:

http://boost.org/doc/html/boost_staticassert.html

Cheers! --M

Jun 27 '06 #2

P: n/a
On Tue, 27 Jun 2006 14:33:34 GMT, Frederick Gotham
<fg*******@SPAM.com> wrote:
Is there any way to force compile failure if a particular compile-time
constant doesn't evaluate to true?
Consider a template which was intended to only work with unsigned
integers (whether they be char, short, int, long):

#include <limits>

template<class T>
void Func()
{
CompileErrorIfTrue( std::numeric_limits<T>::is_signed );

/* Rest of function */
}


Try:

#define COMPILE_TIME_ASSERT(expr) \
typedef char compile_time_constraint[(expr)? 1:-1]

Best wishes,
Roland Pibinger
Jun 27 '06 #3

P: n/a
Roland Pibinger wrote:
#define COMPILE_TIME_ASSERT(expr) \
typedef char compile_time_constraint[(expr)? 1:-1]


Could a template be cleaner and more self-documenting? Consider the error
message thus produced...

--
Phlip
Jun 27 '06 #4

P: n/a
On Tue, 27 Jun 2006 18:15:41 GMT, Phlip <ph*******@gEEEmail.com>
wrote:
Roland Pibinger wrote:
#define COMPILE_TIME_ASSERT(expr) \
typedef char compile_time_constraint[(expr)? 1:-1]
Could a template be cleaner and more self-documenting?


Templates are not known for clean and self-documenting error messages.

Consider the error message thus produced...


The error message of a COMPILE_TIME_ASSERT macro points to the line
where the macro is expanded which should make the constraint violation
detectable at first sight.

Best wishes,
Roland Pibinger
Jun 27 '06 #5

P: n/a
Roland Pibinger wrote:
Phlip wrote:

#define COMPILE_TIME_ASSERT(expr) \
typedef char compile_time_constraint[(expr)? 1:-1]


Could a template be cleaner and more self-documenting?


Templates are not known for clean and self-documenting error messages.


You shifted what I wrote so you could then complain about it.

Google for "One of the aims of BOOST_STATIC_ASSERT is to generate readable
error messages..."

--
Phlip
Jun 27 '06 #6

P: n/a
Roland Pibinger wrote:
On Tue, 27 Jun 2006 14:33:34 GMT, Frederick Gotham
<fg*******@SPAM.com> wrote:
Is there any way to force compile failure if a particular compile-time
constant doesn't evaluate to true?
Consider a template which was intended to only work with unsigned
integers (whether they be char, short, int, long):

#include <limits>

template<class T>
void Func()
{
CompileErrorIfTrue( std::numeric_limits<T>::is_signed );

/* Rest of function */
}


Try:

#define COMPILE_TIME_ASSERT(expr) \
typedef char compile_time_constraint[(expr)? 1:-1]


Can you explain what this does?

Thanks,
Mark
Jun 27 '06 #7

P: n/a
Mark P posted:

#define COMPILE_TIME_ASSERT(expr) \
typedef char compile_time_constraint[(expr)? 1:-1]


Can you explain what this does?

Try to define an array of negative dimensions:
int main()
{
int array[ -1 ];
}
See what happens.
--

Frederick Gotham
Jun 27 '06 #8

P: n/a
On Tue, 27 Jun 2006 18:59:08 GMT, Phlip <ph*******@gEEEmail.com>
wrote:

You shifted what I wrote so you could then complain about it.
Sorry!
Google for "One of the aims of BOOST_STATIC_ASSERT is to generate readable
error messages..."


I hope that Boost also aims to generate readable error messages for
the Boost libraries, someday.
Jun 27 '06 #9

P: n/a
On Tue, 27 Jun 2006 19:11:41 GMT, Mark P
<us****@fall2005REMOVE.fastmailCAPS.fm> wrote:
Roland Pibinger wrote:
Try:

#define COMPILE_TIME_ASSERT(expr) \
typedef char compile_time_constraint[(expr)? 1:-1]


Can you explain what this does?


See: http://www.embedded.com/showArticle....leID=164900888

Best wishes,
Roland Pibinger
Jun 27 '06 #10

P: n/a
Roland Pibinger wrote:
Google for "One of the aims of BOOST_STATIC_ASSERT is to generate
readable error messages..."


I hope that Boost also aims to generate readable error messages for the
Boost libraries, someday.


And they admit they might not generate one for the assertion failure,
either. ;-)

--
Phlip

Jun 27 '06 #11

P: n/a
Frederick Gotham wrote:
Mark P posted:

#define COMPILE_TIME_ASSERT(expr) \
typedef char compile_time_constraint[(expr)? 1:-1]

Can you explain what this does?

Try to define an array of negative dimensions:
int main()
{
int array[ -1 ];
}
See what happens.


Ah, of course, thanks. I'm not used to the syntax for array typedefs
and I missed that.
Jun 27 '06 #12

P: n/a
>> int array[ -1 ];

There are those who would use array[0], which is also illegal. But too
many old compilers permitted the zero.

--
Phlip
Jun 27 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.