Any way to force compile failure?

F

Frederick Gotham

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_signed>();

/* Rest of function */
}

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

mlimber

Frederick said:
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_signed>();

/* Rest of function */
}

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

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

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

Cheers! --M
 
R

Roland Pibinger

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
 
P

Phlip

Roland said:
#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...
 
R

Roland Pibinger

Roland said:
#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
 
P

Phlip

Roland said:
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..."
 
M

Mark P

Roland said:
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
 
F

Frederick Gotham

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

Roland Pibinger

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

Phlip

Roland said:
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. ;-)
 
M

Mark P

Frederick said:
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.
 
P

Phlip

int array[ -1 ];

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

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,175
Messages
2,570,944
Members
47,491
Latest member
mohitk

Latest Threads

Top