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

templates to generate compiler diagnostics

P: n/a
I just want to share a technique I discovered when playing
around with templates. It is a simple idea, so I doubt I
am first, perhaps it's even used in the standard library
implementations, and the boost library.

Anyway, the idea is to use templates as a compile time
assert, letting the compiler fail, with a reasonable
diagnostics if some condition isn't fulfilled.

For example, consider a template function that shifts
variables of the template argument type left and right,
then it would be nice to assure that the template
argument time isn't signed, since right shifting isn't
well defined on negative values:

template <typename IntType>
IntType func(IntType x)
{
/* do operations on x */
x >>= 1; /* what if x < 0? */
return x;
}

My solution is this:

#include "compile_assert.h"
#include <limits>

template <typename IntType>
IntType func(IntType x)
{
compile_assert< ! std::numeric_limits<IntType>::is_signed > ();

/* do operations on x */
x >>= 1; /* what if x < 0? */
return x;
}

The code result in a compiler diagnostic pointing to the compile_assert
line if the function is called with a signed type argument.

Here is the definition of compile_assert:

#ifndef H_COMPILE_ASSERT
#define H_COMPILE_ASSERT

namespace {
template <bool is_ok> struct compile_assert_t;
template <> struct compile_assert_t<true> { };
}

template <bool is_ok> void compile_assert()
{
compile_assert_t<is_ok>();
}

#endif

No question just wanted to share, comments are welcome.

/Niklas Norrthon
Dec 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Niklas Norrthon wrote:
I just want to share a technique I discovered when playing
around with templates. It is a simple idea, so I doubt I
am first, perhaps it's even used in the standard library
implementations, and the boost library.

Anyway, the idea is to use templates as a compile time
assert, letting the compiler fail, with a reasonable
diagnostics if some condition isn't fulfilled.

For example, consider a template function that shifts
variables of the template argument type left and right,
then it would be nice to assure that the template
argument time isn't signed, since right shifting isn't
well defined on negative values:

template <typename IntType>
IntType func(IntType x)
{
/* do operations on x */
x >>= 1; /* what if x < 0? */
return x;
}

My solution is this:

#include "compile_assert.h"
#include <limits>

template <typename IntType>
IntType func(IntType x)
{
compile_assert< ! std::numeric_limits<IntType>::is_signed > ();

/* do operations on x */
x >>= 1; /* what if x < 0? */
return x;
}

The code result in a compiler diagnostic pointing to the compile_assert
line if the function is called with a signed type argument.

Here is the definition of compile_assert:

#ifndef H_COMPILE_ASSERT
#define H_COMPILE_ASSERT

namespace {
template <bool is_ok> struct compile_assert_t;
template <> struct compile_assert_t<true> { };
}

template <bool is_ok> void compile_assert()
{
compile_assert_t<is_ok>();
}

#endif

No question just wanted to share, comments are welcome.

/Niklas Norrthon


Your assert is akin to BOOST_STATIC_ASSERT
(http://www.boost.org/doc/html/boost_staticassert.html) and Loki's
STATIC_CHECK, which are used to great avail in those libraries and
elsewhere. You could also use typetraits from either of those libraries
to check the signedness of a type without using <limits>.

Cheers! --M

Dec 14 '05 #2

P: n/a
mlimber wrote:
[..] You could also use typetraits from either of those libraries
to check the signedness of a type without using <limits>.


Of course one could also use some third-party library to do things that
are already covered by the Standard Library. But WHY?
Dec 14 '05 #3

P: n/a
Victor Bazarov wrote:
mlimber wrote:
[..] You could also use typetraits from either of those libraries
to check the signedness of a type without using <limits>.


Of course one could also use some third-party library to do things that
are already covered by the Standard Library. But WHY?


Fair enough. I would note, however, that Boost.Typetraits are included
in TR1
(http://www.open-std.org/jtc1/sc22/wg...2005/n1745.pdf) and
may someday be part of the standard, thus providing two standard ways
to accomplish the same thing.

Cheers! --M

Dec 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.