Skip to content

Commit

Permalink
optimization: avoid overhead of function call for invalid type check
Browse files Browse the repository at this point in the history
  • Loading branch information
Dane Springmeyer committed Feb 12, 2014
1 parent c65aa11 commit cebd6a3
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace util {

namespace detail {

static constexpr std::size_t invalid_value = std::size_t(-1);

template <typename T, typename...Types>
struct type_traits;

Expand All @@ -31,7 +33,7 @@ struct type_traits<T, First, Types...>
template <typename T>
struct type_traits<T>
{
static constexpr std::size_t id = std::size_t(-1); // invalid
static constexpr std::size_t id = invalid_value;
};

template <typename T, typename...Types>
Expand Down Expand Up @@ -162,18 +164,13 @@ struct variant

using helper_t = variant_helper<Types...>;

VARIANT_INLINE static std::size_t invalid_type()
{
return std::size_t(-1);
}

std::size_t type_id;
data_t data;

public:

VARIANT_INLINE variant()
: type_id(invalid_type()) {}
: type_id(detail::invalid_value) {}

template <typename T>
VARIANT_INLINE explicit variant(T const& val) noexcept
Expand Down Expand Up @@ -218,7 +215,7 @@ struct variant

VARIANT_INLINE void valid()
{
return (type_id != invalid_type());
return (type_id != detail::invalid_value);
}

template<typename T, typename... Args>
Expand Down

0 comments on commit cebd6a3

Please sign in to comment.