Skip to content

Commit

Permalink
Add constexpr_fatal macro for constexpr errors
Browse files Browse the repository at this point in the history
We need a complicated way to report constepxr errors in older gcc
versions because gcc 5.3 didn't quite implement all of C++14 constexpr
properly.

Add a macro that performs the workaround solution on gcc < 6, but uses a
neater solution (including an error message) on other compilers.
  • Loading branch information
jbytheway committed Jul 2, 2019
1 parent 58f0149 commit 6e07ea5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/coordinates.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ constexpr int map_squares_per( scale s )
case scale::overmap:
return OMAPX * map_squares_per( scale::overmap_terrain );
default:
debugmsg( "Requested scale of %d", s );
abort();
constexpr_fatal( 0, "Requested scale of %d", s );
}
}

Expand All @@ -67,8 +66,7 @@ constexpr origin origin_from_scale( scale s )
case scale::overmap:
return origin::overmap;
default:
debugmsg( "Requested origin for of %d", s );
abort();
constexpr_fatal( origin::abs, "Requested origin for scale %d", s );
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ inline void realDebugmsg( const char *const filename, const char *const line,
std::forward<Args>( args )... ) );
}

// A fatal error for use in constexpr functions
// This exists for compatibility reasons. On gcc 5.3 we need a
// different implementation that is messier.
// Pass a placeholder return value to be used on gcc 5.3 (it won't
// actually be returned, it's just needed for the type), and then
// args as if to debugmsg for the remaining args.
#if defined(__GNUC__) && __GNUC__ < 6
#define constexpr_fatal(ret, ...) \
do { return false ? ( ret ) : ( abort(), ( ret ) ); } while(false)
#else
#define constexpr_fatal(ret, ...) \
do { debugmsg(__VA_ARGS__); abort(); } while(false)
#endif

/**
* Used to generate game report information.
*/
Expand Down

0 comments on commit 6e07ea5

Please sign in to comment.