473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Any way to force compile failure?


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
12 4510
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
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
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
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
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
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
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
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
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
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
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
>> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: T. F. | last post by:
Hi, i'm creating a website which uses a very special font that not all people have on their computer. Without this font the site looks just ridiculous. Of course I want everyone to see the site...
12
by: DJ | last post by:
I have a .NET datagrid that has a column of dropdownlist controls. If the user changes the value in the dropdown in one or more rows I will set a flag. If the user subsequently attempts to (1)...
5
by: Carmine Cairo | last post by:
Hi, I'm working on a project and today I've note a little problem during the compile fase. Here a little piece of code: // 1st version welldone = 0; size = p->getSize(); backbone = new...
1
by: yancheng.cheok | last post by:
Hi all, According to "How can I handle a constructor that fails?" in http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.2, whenever there is a constructor fail, we will throw...
5
by: Dave Booker | last post by:
I am developing in VS2005, but I have some third-party DLL's that appear to only work correctly when compiled in VS2003. ("Incorrect" operation is defined as a busy-wait that kicks in on a message...
0
by: Verticon:: | last post by:
Per microsoft, the ExitCode property of the ServiceBase returns 0 for success and non zero for failure. However, when I set this property to 1, the service still stops in a success state. ...
3
by: Joachim Klassen | last post by:
Hi all, if I accidentally use a TAKEOVER command with BY FORCE clause while primary and standby are in peer state I'll end up with two primary's (at least with FP10 and Windows). Is this works ...
3
by: Dean Slindee | last post by:
My project is getting a bit large, and I would like to kick-start VS05 into performing the error-checking compilation at my direction, rather than waiting for it to self-start. Is there a way to...
2
by: mark4asp | last post by:
Can I force the client to stop caching old stylesheets and javascript? In my dynamic web-site, I need to force the client to stop caching old versions of my stylesheets and javascript. Can I do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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...

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.