Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
5206a614
Commit
5206a614
authored
Nov 27, 2014
by
Davis King
Browse files
Made the compile time assert macros use static_assert if it's available.
parent
db3eaa72
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
3 deletions
+28
-3
dlib/assert.h
dlib/assert.h
+28
-3
No files found.
dlib/assert.h
View file @
5206a614
...
@@ -26,6 +26,20 @@
...
@@ -26,6 +26,20 @@
#define BOOST_DO_JOIN2( X, Y ) X##Y
#define BOOST_DO_JOIN2( X, Y ) X##Y
#endif
#endif
// figure out if the compiler has static_assert.
#if defined(__clang__)
# if __has_feature(cxx_static_assert)
# define DLIB_HAS_STATIC_ASSERT
# endif
#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)) && defined(__GXX_EXPERIMENTAL_CXX0X__)
# define DLIB_HAS_STATIC_ASSERT
#elif defined(_MSC_VER) && _MSC_VER >= 1600
# define DLIB_HAS_STATIC_ASSERT
#elif defined(__INTEL_COMPILER) && defined(BOOST_INTEL_STDCXX0X)
# define DLIB_HAS_STATIC_ASSERT
#endif
// -----------------------------
// -----------------------------
namespace
dlib
namespace
dlib
...
@@ -37,6 +51,9 @@ namespace dlib
...
@@ -37,6 +51,9 @@ namespace dlib
template
<
typename
T
>
struct
assert_are_same_type
<
T
,
T
>
{
enum
{
value
=
1
};};
template
<
typename
T
>
struct
assert_are_same_type
<
T
,
T
>
{
enum
{
value
=
1
};};
template
<
typename
T
,
typename
U
>
struct
assert_are_not_same_type
{
enum
{
value
=
1
};
};
template
<
typename
T
,
typename
U
>
struct
assert_are_not_same_type
{
enum
{
value
=
1
};
};
template
<
typename
T
>
struct
assert_are_not_same_type
<
T
,
T
>
{};
template
<
typename
T
>
struct
assert_are_not_same_type
<
T
,
T
>
{};
template
<
typename
T
,
typename
U
>
struct
assert_types_match
{
enum
{
value
=
0
};};
template
<
typename
T
>
struct
assert_types_match
<
T
,
T
>
{
enum
{
value
=
1
};};
}
}
...
@@ -48,14 +65,22 @@ namespace dlib
...
@@ -48,14 +65,22 @@ namespace dlib
#define DLIB_NO_WARN_UNUSED
#define DLIB_NO_WARN_UNUSED
#endif
#endif
#define COMPILE_TIME_ASSERT(expression) \
// Use the newer static_assert if it's avaiable since it produces much more readable error
// messages.
#ifdef DLIB_HAS_STATIC_ASSERT
#define COMPILE_TIME_ASSERT(expression) static_assert(expression, "Failed assertion")
#define ASSERT_ARE_SAME_TYPE(type1, type2) static_assert(assert_types_match<type1,type2>::value, "These types should be the same but aren't.")
#define ASSERT_ARE_NOT_SAME_TYPE(type1, type2) static_assert(!assert_types_match<type1,type2>::value, "These types should NOT be the same.")
#else
#define COMPILE_TIME_ASSERT(expression) \
DLIB_NO_WARN_UNUSED typedef char BOOST_JOIN(DLIB_CTA, __LINE__)[::dlib::compile_time_assert<(bool)(expression)>::value]
DLIB_NO_WARN_UNUSED typedef char BOOST_JOIN(DLIB_CTA, __LINE__)[::dlib::compile_time_assert<(bool)(expression)>::value]
#define ASSERT_ARE_SAME_TYPE(type1, type2) \
#define ASSERT_ARE_SAME_TYPE(type1, type2) \
DLIB_NO_WARN_UNUSED typedef char BOOST_JOIN(DLIB_AAST, __LINE__)[::dlib::assert_are_same_type<type1,type2>::value]
DLIB_NO_WARN_UNUSED typedef char BOOST_JOIN(DLIB_AAST, __LINE__)[::dlib::assert_are_same_type<type1,type2>::value]
#define ASSERT_ARE_NOT_SAME_TYPE(type1, type2) \
#define ASSERT_ARE_NOT_SAME_TYPE(type1, type2) \
DLIB_NO_WARN_UNUSED typedef char BOOST_JOIN(DLIB_AANST, __LINE__)[::dlib::assert_are_not_same_type<type1,type2>::value]
DLIB_NO_WARN_UNUSED typedef char BOOST_JOIN(DLIB_AANST, __LINE__)[::dlib::assert_are_not_same_type<type1,type2>::value]
#endif
// -----------------------------
// -----------------------------
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment