Skip to content

Commit

Permalink
Fix bug with List where replace() doesn't work with non-pointer types
Browse files Browse the repository at this point in the history
  • Loading branch information
timwoj committed Apr 30, 2020
1 parent fda9498 commit 615f8cd
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/List.h
Expand Up @@ -240,16 +240,16 @@ class List {
T replace(int ent_index, const T& new_ent) // replace entry #i with a new value
{
if ( ent_index < 0 )
return 0;
return get_default_value();

T old_ent = nullptr;
T old_ent = get_default_value();

if ( ent_index > num_entries - 1 )
{ // replacement beyond the end of the list
resize(ent_index + 1);

for ( int i = num_entries; i < max_entries; ++i )
entries[i] = nullptr;
entries[i] = get_default_value();
num_entries = max_entries;
}
else
Expand Down Expand Up @@ -287,6 +287,20 @@ class List {

protected:

T get_default_value()
{
if constexpr ( std::is_pointer_v<T> )
return nullptr;
else if constexpr ( std::is_integral_v<T> )
return 0;
else if constexpr ( std::is_floating_point_v<T> )
return 0.0;
else
// TODO: Should this return an error at compile time that we don't
// know what this type is?
return T();
}

// This could essentially be an std::vector if we wanted. Some
// reasons to maybe not refactor to use std::vector ?
//
Expand Down

0 comments on commit 615f8cd

Please sign in to comment.