Skip to content

Commit b8ab624

Browse files
NathanJPhillipssmowton
authored andcommitted
Code review fixup
Added missing documentation of type parameter Added static assert for type of TExpr
1 parent 775d9dd commit b8ab624

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/util/expr_cast.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/// \brief Templated functions to cast to specific exprt-derived classes
1010

1111
#include <typeinfo>
12+
#include <type_traits>
1213
#include "invariant.h"
1314
#include "expr.h"
1415

@@ -56,12 +57,16 @@ T expr_dynamic_cast(exprt *base)
5657
/// \brief Cast a pointer to a generic exprt to a specific derived class
5758
/// \tparam T The pointer or const pointer type to \a TUnderlying to cast to
5859
/// \tparam TUnderlying An exprt-derived class type
60+
/// \tparam TExpr The original type to cast from, either exprt or const exprt
5961
/// \param base Pointer to a generic \ref exprt
6062
/// \return Pointer to object of type \a TUnderlying
6163
/// or null if \a base is not an instance of \a TUnderlying
6264
template<typename T, typename TUnderlying, typename TExpr>
6365
T expr_dynamic_cast(TExpr *base)
6466
{
67+
static_assert(
68+
std::is_same<typename std::remove_const<TExpr>::type, exprt>::value,
69+
"Tried to expr_dynamic_cast from something that wasn't an exprt");
6570
static_assert(
6671
std::is_pointer<T>::value,
6772
"Tried to convert exprt * to non-pointer type");
@@ -109,12 +114,16 @@ T expr_dynamic_cast(exprt &base)
109114
/// \brief Cast a reference to a generic exprt to a specific derived class
110115
/// \tparam T The reference or const reference type to \a TUnderlying to cast to
111116
/// \tparam TUnderlying An exprt-derived class type
117+
/// \tparam TExpr The original type to cast from, either exprt or const exprt
112118
/// \param base Reference to a generic \ref exprt
113119
/// \return Reference to object of type \a T
114120
/// \throw std::bad_cast If \a base is not an instance of \a TUnderlying
115121
template<typename T, typename TUnderlying, typename TExpr>
116122
T expr_dynamic_cast(TExpr &base)
117123
{
124+
static_assert(
125+
std::is_same<typename std::remove_const<TExpr>::type, exprt>::value,
126+
"Tried to expr_dynamic_cast from something that wasn't an exprt");
118127
static_assert(
119128
std::is_reference<T>::value,
120129
"Tried to convert exprt & to non-reference type");
@@ -133,10 +142,10 @@ inline void validate_operands(
133142
const exprt &value,
134143
exprt::operandst::size_type number,
135144
const char *message,
136-
bool allowMore=false)
145+
bool allow_more=false)
137146
{
138147
DATA_INVARIANT(
139-
allowMore
148+
allow_more
140149
? value.operands().size()==number
141150
: value.operands().size()>=number,
142151
message);

0 commit comments

Comments
 (0)