[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master f05c8c5 4/4: Avoid error-prone mixing of sign
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master f05c8c5 4/4: Avoid error-prone mixing of signed and unsigned |
Date: |
Mon, 4 Jun 2018 12:19:53 -0400 (EDT) |
branch: master
commit f05c8c535267e88c514f26be7b763a176df80439
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Avoid error-prone mixing of signed and unsigned
Resolved all warnings that '-Wsign-conversion' would elicit in a 32-bit
compile with i686-w64-mingw32-gcc-7.3, except the very large number that
grep 'error:' |sed -e '/size_type/d'
filters out (notably due to the absence of 'vector::operator[](int)').
* fenv_lmi_x86.hpp: Incidentally used std::bitset::operator[] uniformly,
removing a workaround for an ancient compiler.
* ihs_acctval.cpp: Replaced assertion + operator[] with at().
* mc_enum_metadata.hpp: Removed a puzzling comment. Rewrote a static
assertion to clarify its actual meaning.
* miscellany.cpp: In count_newlines(), it is not necessarily ideal to
return the result of std::count() as an int, but returning std::size_t
certainly involved an implicit sign conversion. (The same may be said
of returning std::ptrdiff_t as int in several other files.)
* test_coding_rules.cpp: Incidentally used an initializer-list for a
std::set; the same technique (N1509) should be used elsewhere.
* timer.cpp: Preferred 'int' for lmi's own timing function, casting it
as needed for OS calls. Sleeping for more than 2^31 seconds is not a
realistic use case; of course, bourn_cast<>() throws an exception if
anyone tries to do that.
* vector_test.cpp: Removed casts that, although correct, were curiously
used only on the second and third of three similar successive lines.
---
actuarial_table.cpp | 9 +++++----
bourn_cast.hpp | 1 +
ce_product_name.cpp | 14 ++++++++------
ce_product_name.hpp | 7 +++----
ce_skin_name.cpp | 14 ++++++++------
ce_skin_name.hpp | 7 +++----
crc32.hpp | 4 ++--
fenv_lmi_test.cpp | 2 +-
fenv_lmi_x86.hpp | 38 +++++++++++++++++---------------------
group_values.cpp | 11 ++++++-----
ihs_acctval.cpp | 8 ++------
ihs_irc7702a.cpp | 11 ++++++-----
input_realization.cpp | 9 +++++----
mc_enum.cpp | 4 ++--
mc_enum.hpp | 15 +++++++--------
mc_enum.tpp | 17 +++++++++--------
mc_enum_metadata.hpp | 10 ++++------
mc_enum_types_aux.cpp | 3 +--
miscellany.cpp | 5 +++--
miscellany.hpp | 4 ++--
mvc_controller.cpp | 7 +++----
numeric_io_traits.hpp | 2 +-
test_coding_rules.cpp | 13 +++++--------
timer.cpp | 6 ++++--
timer.hpp | 2 +-
vector_test.cpp | 5 ++---
26 files changed, 111 insertions(+), 117 deletions(-)
diff --git a/actuarial_table.cpp b/actuarial_table.cpp
index d5a96b7..c8babd1 100644
--- a/actuarial_table.cpp
+++ b/actuarial_table.cpp
@@ -25,6 +25,7 @@
#include "alert.hpp"
#include "assert_lmi.hpp"
+#include "bourn_cast.hpp"
#include "deserialize_cast.hpp"
#include "miscellany.hpp"
#include "oecumenic_enumerations.hpp" // methuselah
@@ -65,7 +66,7 @@ namespace
/// in order to avoid warnings for unsigned types.
template<typename T>
- T read_datum(std::istream& is, T& t, std::int16_t nominal_length)
+ T read_datum(std::istream& is, T& t, std::uint16_t nominal_length)
{
LMI_ASSERT(sizeof(T) == nominal_length);
T const invalid(static_cast<T>(-1));
@@ -331,9 +332,9 @@ void actuarial_table::parse_table()
// Meaning: {A, D, S} --> {age, duration, select}.
// SOA apparently permits upper or lower case.
LMI_ASSERT(-1 == table_type_);
- unsigned char z;
+ char z;
read_datum(data_ifs, z, nominal_length);
- z = static_cast<unsigned char>(std::toupper(z));
+ z = bourn_cast<char>(std::toupper(z));
LMI_ASSERT('A' == z || 'D' == z || 'S' == z);
table_type_ = z;
}
@@ -452,7 +453,7 @@ void actuarial_table::read_values(std::istream& is, int
nominal_length)
+ 1 + max_age_ - min_age_ - select_period_
;
}
- int deduced_length = number_of_values * sizeof(double);
+ int deduced_length = number_of_values * static_cast<int>(sizeof(double));
LMI_ASSERT
( soa_table_length_max < deduced_length
|| nominal_length == deduced_length
diff --git a/bourn_cast.hpp b/bourn_cast.hpp
index b48e443..4135fb6 100644
--- a/bourn_cast.hpp
+++ b/bourn_cast.hpp
@@ -32,6 +32,7 @@
#if defined __GNUC__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wsign-compare"
+# pragma GCC diagnostic ignored "-Wsign-conversion"
# if 5 <= __GNUC__
# pragma GCC diagnostic ignored "-Wbool-compare"
# endif // 5 <= __GNUC__
diff --git a/ce_product_name.cpp b/ce_product_name.cpp
index 2c18cf2..9b535c4 100644
--- a/ce_product_name.cpp
+++ b/ce_product_name.cpp
@@ -28,12 +28,14 @@
#include "facets.hpp"
#include "global_settings.hpp"
#include "path_utility.hpp" // fs::path inserter
+#include "ssize_lmi.hpp"
#include <boost/filesystem/convenience.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <algorithm> // find()
+#include <cstddef> // ptrdiff_t
namespace
{
@@ -120,9 +122,9 @@ bool ce_product_name::operator==(std::string const& s) const
return s == str();
}
-std::size_t ce_product_name::ordinal(std::string const& s)
+int ce_product_name::ordinal(std::string const& s)
{
- std::size_t v =
+ std::ptrdiff_t v =
std::find
(product_names().begin()
,product_names().end()
@@ -130,7 +132,7 @@ std::size_t ce_product_name::ordinal(std::string const& s)
)
- product_names().begin()
;
- if(v == product_names().size())
+ if(v == lmi::ssize(product_names()))
{
alarum()
<< "Value '"
@@ -149,9 +151,9 @@ std::vector<std::string> const&
ce_product_name::all_strings() const
return product_names();
}
-std::size_t ce_product_name::cardinality() const
+int ce_product_name::cardinality() const
{
- return product_names().size();
+ return lmi::ssize(product_names());
}
/// No product is ever proscribed.
@@ -159,7 +161,7 @@ std::size_t ce_product_name::cardinality() const
void ce_product_name::enforce_proscription()
{}
-std::size_t ce_product_name::ordinal() const
+int ce_product_name::ordinal() const
{
return ordinal(value_);
}
diff --git a/ce_product_name.hpp b/ce_product_name.hpp
index 88d14ea..efeb728 100644
--- a/ce_product_name.hpp
+++ b/ce_product_name.hpp
@@ -26,7 +26,6 @@
#include "mc_enum.hpp"
-#include <cstddef> // size_t
#include <string>
#include <vector>
@@ -63,13 +62,13 @@ class ce_product_name
bool operator==(ce_product_name const&) const;
bool operator==(std::string const&) const;
- static std::size_t ordinal(std::string const&);
+ static int ordinal(std::string const&);
// mc_enum_base required implementation.
std::vector<std::string> const& all_strings() const override;
- std::size_t cardinality() const override;
+ int cardinality() const override;
void enforce_proscription() override;
- std::size_t ordinal() const override;
+ int ordinal() const override;
std::string str(int) const override;
std::string str() const;
diff --git a/ce_skin_name.cpp b/ce_skin_name.cpp
index b3d376f..949f3c5 100644
--- a/ce_skin_name.cpp
+++ b/ce_skin_name.cpp
@@ -29,12 +29,14 @@
#include "global_settings.hpp"
#include "miscellany.hpp" // begins_with()
#include "path_utility.hpp" // fs::path inserter
+#include "ssize_lmi.hpp"
#include <boost/filesystem/convenience.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <algorithm> // find()
+#include <cstddef> // ptrdiff_t
namespace
{
@@ -117,9 +119,9 @@ bool ce_skin_name::operator==(std::string const& s) const
return s == str();
}
-std::size_t ce_skin_name::ordinal(std::string const& s)
+int ce_skin_name::ordinal(std::string const& s)
{
- std::size_t v =
+ std::ptrdiff_t v =
std::find
(skin_names().begin()
,skin_names().end()
@@ -127,7 +129,7 @@ std::size_t ce_skin_name::ordinal(std::string const& s)
)
- skin_names().begin()
;
- if(v == skin_names().size())
+ if(v == lmi::ssize(skin_names()))
{
alarum()
<< "Value '"
@@ -146,9 +148,9 @@ std::vector<std::string> const& ce_skin_name::all_strings()
const
return skin_names();
}
-std::size_t ce_skin_name::cardinality() const
+int ce_skin_name::cardinality() const
{
- return skin_names().size();
+ return lmi::ssize(skin_names());
}
/// No skin is ever proscribed.
@@ -156,7 +158,7 @@ std::size_t ce_skin_name::cardinality() const
void ce_skin_name::enforce_proscription()
{}
-std::size_t ce_skin_name::ordinal() const
+int ce_skin_name::ordinal() const
{
return ordinal(value_);
}
diff --git a/ce_skin_name.hpp b/ce_skin_name.hpp
index b1d8787..11df5c3 100644
--- a/ce_skin_name.hpp
+++ b/ce_skin_name.hpp
@@ -26,7 +26,6 @@
#include "mc_enum.hpp"
-#include <cstddef> // size_t
#include <string>
#include <vector>
@@ -55,13 +54,13 @@ class ce_skin_name
bool operator==(ce_skin_name const&) const;
bool operator==(std::string const&) const;
- static std::size_t ordinal(std::string const&);
+ static int ordinal(std::string const&);
// mc_enum_base required implementation.
std::vector<std::string> const& all_strings() const override;
- std::size_t cardinality() const override;
+ int cardinality() const override;
void enforce_proscription() override;
- std::size_t ordinal() const override;
+ int ordinal() const override;
std::string str(int) const override;
std::string str() const;
diff --git a/crc32.hpp b/crc32.hpp
index bdef6b3..e3b673a 100644
--- a/crc32.hpp
+++ b/crc32.hpp
@@ -25,8 +25,8 @@
#include "config.hpp"
#include "so_attributes.hpp"
+#include "ssize_lmi.hpp"
-#include <cstring> // strlen()
#include <string>
#include <vector>
@@ -110,7 +110,7 @@ inline CRC& CRC::operator+=(unsigned char const*
z)
{
// std::strlen() is defined only for char const* arguments, so the
// cast is required.
- return update(z, std::strlen(reinterpret_cast<char const*>(z)));
+ return update(z, lmi::sstrlen(reinterpret_cast<char const*>(z)));
}
template<typename T>
diff --git a/fenv_lmi_test.cpp b/fenv_lmi_test.cpp
index fc78eb3..461f9b9 100644
--- a/fenv_lmi_test.cpp
+++ b/fenv_lmi_test.cpp
@@ -95,7 +95,7 @@ int test_main(int, char*[])
BOOST_TEST_EQUAL_BITS
(intel_control_word_parameters::reserved_values
- ,~0x01000 & x87_control_word()
+ ,~0x01000UL & x87_control_word()
);
// Make sure invalid PC values are forbidden. The implementation
diff --git a/fenv_lmi_x86.hpp b/fenv_lmi_x86.hpp
index d7a4a31..c108a73 100644
--- a/fenv_lmi_x86.hpp
+++ b/fenv_lmi_x86.hpp
@@ -220,7 +220,7 @@ class control_word
typedef typename std::bitset<ControlWordType::nbits>::reference ref_type;
public:
- control_word(int w)
+ control_word(unsigned long int w)
{
cw_ = ControlWordType::reserved_values | ControlWordType::settable & w;
}
@@ -253,31 +253,27 @@ class control_word
std::bitset<ControlWordType::nbits> const& bits() const {return cw_;}
private:
- ref_type im() {return cw_[ControlWordType::im_bit];}
- ref_type dm() {return cw_[ControlWordType::dm_bit];}
- ref_type zm() {return cw_[ControlWordType::zm_bit];}
- ref_type om() {return cw_[ControlWordType::om_bit];}
- ref_type um() {return cw_[ControlWordType::um_bit];}
- ref_type pm() {return cw_[ControlWordType::pm_bit];}
+ ref_type im() {return cw_[ ControlWordType::im_bit ];}
+ ref_type dm() {return cw_[ ControlWordType::dm_bit ];}
+ ref_type zm() {return cw_[ ControlWordType::zm_bit ];}
+ ref_type om() {return cw_[ ControlWordType::om_bit ];}
+ ref_type um() {return cw_[ ControlWordType::um_bit ];}
+ ref_type pm() {return cw_[ ControlWordType::pm_bit ];}
ref_type pc0() {return cw_[0 + ControlWordType::pc_bit0];}
ref_type pc1() {return cw_[1 + ControlWordType::pc_bit0];}
ref_type rc0() {return cw_[0 + ControlWordType::rc_bit0];}
ref_type rc1() {return cw_[1 + ControlWordType::rc_bit0];}
- // Not every implementation yet has operator[](std::size_t) const
- // http://www.comeaucomputing.com/iso/lwg-defects.html#11
- // so test() is used where operator[] would suffice.
-
- bool im() const {return cw_.test(ControlWordType::im_bit);}
- bool dm() const {return cw_.test(ControlWordType::dm_bit);}
- bool zm() const {return cw_.test(ControlWordType::zm_bit);}
- bool om() const {return cw_.test(ControlWordType::om_bit);}
- bool um() const {return cw_.test(ControlWordType::um_bit);}
- bool pm() const {return cw_.test(ControlWordType::pm_bit);}
- bool pc0() const {return cw_.test(0 + ControlWordType::pc_bit0);}
- bool pc1() const {return cw_.test(1 + ControlWordType::pc_bit0);}
- bool rc0() const {return cw_.test(0 + ControlWordType::rc_bit0);}
- bool rc1() const {return cw_.test(1 + ControlWordType::rc_bit0);}
+ bool im() const {return cw_[ ControlWordType::im_bit ];}
+ bool dm() const {return cw_[ ControlWordType::dm_bit ];}
+ bool zm() const {return cw_[ ControlWordType::zm_bit ];}
+ bool om() const {return cw_[ ControlWordType::om_bit ];}
+ bool um() const {return cw_[ ControlWordType::um_bit ];}
+ bool pm() const {return cw_[ ControlWordType::pm_bit ];}
+ bool pc0() const {return cw_[0 + ControlWordType::pc_bit0];}
+ bool pc1() const {return cw_[1 + ControlWordType::pc_bit0];}
+ bool rc0() const {return cw_[0 + ControlWordType::rc_bit0];}
+ bool rc1() const {return cw_[1 + ControlWordType::rc_bit0];}
std::bitset<ControlWordType::nbits> cw_;
};
diff --git a/group_values.cpp b/group_values.cpp
index f66611e..313597b 100644
--- a/group_values.cpp
+++ b/group_values.cpp
@@ -37,6 +37,7 @@
#include "mc_enum_types_aux.hpp" // mc_str()
#include "path_utility.hpp"
#include "progress_meter.hpp"
+#include "ssize_lmi.hpp"
#include "timer.hpp"
#include "value_cast.hpp"
@@ -119,7 +120,7 @@ census_run_result run_census_in_series::operator()
census_run_result result;
std::shared_ptr<progress_meter> meter
(create_progress_meter
- (cells.size()
+ (lmi::ssize(cells)
,"Calculating all cells"
,progress_meter_mode(emission)
)
@@ -128,7 +129,7 @@ census_run_result run_census_in_series::operator()
ledger_emitter emitter(file, emission);
result.seconds_for_output_ += emitter.initiate();
- for(unsigned int j = 0; j < cells.size(); ++j)
+ for(int j = 0; j < lmi::ssize(cells); ++j)
{
if(!cell_should_be_ignored(cells[j]))
{
@@ -242,7 +243,7 @@ census_run_result run_census_in_parallel::operator()
census_run_result result;
std::shared_ptr<progress_meter> meter
(create_progress_meter
- (cells.size()
+ (lmi::ssize(cells)
,"Initializing all cells"
,progress_meter_mode(emission)
)
@@ -607,7 +608,7 @@ census_run_result run_census_in_parallel::operator()
} // End for.
meter = create_progress_meter
- (cell_values.size()
+ (lmi::ssize(cell_values)
,"Finalizing all cells"
,progress_meter_mode(emission)
);
@@ -627,7 +628,7 @@ census_run_result run_census_in_parallel::operator()
result.seconds_for_output_ += emitter.initiate();
meter = create_progress_meter
- (cell_values.size()
+ (lmi::ssize(cell_values)
,"Writing output for all cells"
,progress_meter_mode(emission)
);
diff --git a/ihs_acctval.cpp b/ihs_acctval.cpp
index bb808d5..414a6c3 100644
--- a/ihs_acctval.cpp
+++ b/ihs_acctval.cpp
@@ -1855,9 +1855,7 @@ double AccountValue::InforceLivesBoy() const
return 0.0;
}
- unsigned int index = Year;
- LMI_ASSERT(index < InvariantValues().InforceLives.size());
- return InvariantValues().InforceLives[index];
+ return InvariantValues().InforceLives.at(Year);
}
//============================================================================
@@ -1871,9 +1869,7 @@ double AccountValue::InforceLivesEoy() const
return 0.0;
}
- unsigned int index = 1 + Year;
- LMI_ASSERT(index < InvariantValues().InforceLives.size());
- return InvariantValues().InforceLives[index];
+ return InvariantValues().InforceLives.at(1 + Year);
}
//============================================================================
diff --git a/ihs_irc7702a.cpp b/ihs_irc7702a.cpp
index d5424a0..884914e 100644
--- a/ihs_irc7702a.cpp
+++ b/ihs_irc7702a.cpp
@@ -33,6 +33,7 @@
#include "assert_lmi.hpp"
#include "materially_equal.hpp"
#include "miscellany.hpp" // minmax
+#include "ssize_lmi.hpp"
#include "stratified_algorithms.hpp" // TieredNetToGross()
#include <algorithm>
@@ -232,26 +233,26 @@ void Irc7702A::Initialize7702A
SavedAVBeforeMatChg = a_AVBeforeMatChg;
- unsigned int max_years =
+ int max_years =
std::min(a_EndtAge, statutory_max_endowment_age)
- a_IssueAge
;
// TODO ?? TAXATION !! Do we really need '1 +'?
- unsigned int max_dur = 1 + months_per_year * max_years;
+ int max_dur = 1 + months_per_year * max_years;
Pmts.assign(max_dur, 0.0);
Bfts.assign(max_dur, 0.0);
- LMI_ASSERT(a_Pmts.size() <= max_years);
+ LMI_ASSERT(lmi::ssize(a_Pmts) <= max_years);
for(unsigned int j = 0; j < a_Pmts.size(); ++j)
{
// TODO ?? TAXATION !! OK to treat premium history as annual?
Pmts[j * months_per_year] = a_Pmts[j];
}
- LMI_ASSERT(a_Bfts.size() <= max_years);
+ LMI_ASSERT(lmi::ssize(a_Bfts) <= max_years);
// TAXATION !! UpdateBft7702A() updates this, thus:
// Bfts[TestPeriodDur] = current_bft;
// so should we make sure Bfts[TestPeriodDur] is zero here?
- for(unsigned int j = 0; j < a_Bfts.size(); ++j)
+ for(int j = 0; j < lmi::ssize(a_Bfts); ++j)
{
for(int k = 0; k < months_per_year; ++k)
{
diff --git a/input_realization.cpp b/input_realization.cpp
index affce59..596cc84 100644
--- a/input_realization.cpp
+++ b/input_realization.cpp
@@ -33,6 +33,7 @@
#include "input_sequence_aux.hpp" // convert_vector()
#include "miscellany.hpp" // each_equal(), minmax
#include "round_to.hpp"
+#include "ssize_lmi.hpp"
#include "value_cast.hpp"
#include <algorithm>
@@ -967,11 +968,11 @@ int Input::must_overwrite_specamt_with_obsolete_history
bool history_differs = false;
bool future_differs = false;
- unsigned int const years_of_history = InforceYear.value() + (0 !=
InforceMonth.value());
- LMI_ASSERT(years_of_history <= u.size());
- LMI_ASSERT(years_of_history <= v.size());
+ int const years_of_history = InforceYear.value() + (0 !=
InforceMonth.value());
+ LMI_ASSERT(years_of_history <= lmi::ssize(u));
+ LMI_ASSERT(years_of_history <= lmi::ssize(v));
- for(unsigned int j = 0; j < years_of_history; ++j)
+ for(int j = 0; j < years_of_history; ++j)
{
if(u[j] != v[j])
{
diff --git a/mc_enum.cpp b/mc_enum.cpp
index 8ea9491..62ac13c 100644
--- a/mc_enum.cpp
+++ b/mc_enum.cpp
@@ -45,13 +45,13 @@ void mc_enum_base::allow(int index, bool b)
void mc_enum_base::allow_all(bool b)
{
- for(std::size_t j = 0; j < cardinality(); ++j)
+ for(int j = 0; j < cardinality(); ++j)
{
allow(j, b);
}
}
-std::size_t mc_enum_base::first_allowed_ordinal() const
+int mc_enum_base::first_allowed_ordinal() const
{
return std::find(allowed_.begin(), allowed_.end(), true) -
allowed_.begin();
}
diff --git a/mc_enum.hpp b/mc_enum.hpp
index 17bfe2f..4698ffc 100644
--- a/mc_enum.hpp
+++ b/mc_enum.hpp
@@ -26,7 +26,6 @@
#include "datum_base.hpp"
-#include <cstddef> // size_t
#include <deque>
#include <string>
#include <type_traits>
@@ -58,13 +57,13 @@ class LMI_SO mc_enum_base
void allow(int, bool);
void allow_all(bool);
- std::size_t first_allowed_ordinal() const;
+ int first_allowed_ordinal() const;
bool is_allowed(int) const;
virtual std::vector<std::string> const& all_strings() const = 0;
- virtual std::size_t cardinality() const = 0;
+ virtual int cardinality() const = 0;
virtual void enforce_proscription() = 0;
- virtual std::size_t ordinal() const = 0;
+ virtual int ordinal() const = 0;
virtual std::string str(int) const = 0;
private:
@@ -115,20 +114,20 @@ class mc_enum
bool operator!=(T ) const;
bool operator!=(std::string const&) const;
- static std::size_t ordinal(std::string const&);
+ static int ordinal(std::string const&);
// mc_enum_base required implementation.
std::vector<std::string> const& all_strings() const override;
- std::size_t cardinality() const override;
+ int cardinality() const override;
void enforce_proscription() override;
- std::size_t ordinal() const override;
+ int ordinal() const override;
std::string str(int) const override;
std::string str() const;
T value() const;
private:
- static std::size_t n();
+ static int n();
static T const* e();
static char const* const* c();
static std::vector<std::string> const& s();
diff --git a/mc_enum.tpp b/mc_enum.tpp
index ec90305..bd92346 100644
--- a/mc_enum.tpp
+++ b/mc_enum.tpp
@@ -27,6 +27,7 @@
#include "rtti_lmi.hpp"
#include <algorithm> // find()
+#include <cstddef> // ptrdiff_t
#include <typeinfo>
/// The header that defines class mc_enum is by design unaware of its
@@ -105,9 +106,9 @@ bool mc_enum<T>::operator!=(std::string const& s) const
}
template<typename T>
-std::size_t mc_enum<T>::ordinal(std::string const& s)
+int mc_enum<T>::ordinal(std::string const& s)
{
- std::size_t v = std::find(c(), c() + n(), s) - c();
+ std::ptrdiff_t v = std::find(c(), c() + n(), s) - c();
if(v == n())
{
alarum()
@@ -129,7 +130,7 @@ std::vector<std::string> const& mc_enum<T>::all_strings()
const
}
template<typename T>
-std::size_t mc_enum<T>::cardinality() const
+int mc_enum<T>::cardinality() const
{
return n();
}
@@ -142,7 +143,7 @@ void mc_enum<T>::enforce_proscription()
return;
}
- std::size_t z = first_allowed_ordinal();
+ int z = first_allowed_ordinal();
if(z < cardinality())
{
value_ = e()[z];
@@ -150,9 +151,9 @@ void mc_enum<T>::enforce_proscription()
}
template<typename T>
-std::size_t mc_enum<T>::ordinal() const
+int mc_enum<T>::ordinal() const
{
- std::size_t i = std::find(e(), e() + n(), value_) - e();
+ std::ptrdiff_t i = std::find(e(), e() + n(), value_) - e();
if(i == n())
{
alarum()
@@ -189,7 +190,7 @@ T mc_enum<T>::value() const
// e() and in c() are unique? Can that be asserted at compile time?
template<typename T>
-std::size_t mc_enum<T>::n() {return mc_enum_key<T>::n_;}
+int mc_enum<T>::n() {return mc_enum_key<T>::n_;}
template<typename T>
T const* mc_enum<T>::e() {return mc_enum_key<T>::e();}
@@ -237,7 +238,7 @@ std::istream& mc_enum<T>::read(std::istream& is)
is >> s;
is.imbue(old_locale);
- std::size_t v = std::find(c(), c() + n(), s) - c();
+ std::ptrdiff_t v = std::find(c(), c() + n(), s) - c();
if(n() == v)
{
v = std::find(c(), c() + n(), provide_for_backward_compatibility(s)) -
c();
diff --git a/mc_enum_metadata.hpp b/mc_enum_metadata.hpp
index 7ad279c..2a76cd8 100644
--- a/mc_enum_metadata.hpp
+++ b/mc_enum_metadata.hpp
@@ -102,16 +102,14 @@ struct mc_enum_key
/// and writing them outside the struct definition would be balky.
///
/// 'n' could have been provided as a function rather than a constant,
-/// but a constant is preferable because it can be used in an ICE. It
-/// could have been an enumeration, but declaring it as a std::size_t
-/// costs no more and makes its type definite and explicit.
+/// but a constant is preferable because it can be used in an ICE.
-template<typename T, std::size_t N, T const (&E)[N], char const*const (&C)[N]>
+template<typename T, std::size_t n, T const (&E)[n], char const*const (&C)[n]>
struct mc_enum_data
{
- static_assert(0 < N);
+ static_assert(0 != n);
- static std::size_t const n_ = N;
+ static int const n_ {n};
static T const* e() {return E;}
static char const* const* c() {return C;}
};
diff --git a/mc_enum_types_aux.cpp b/mc_enum_types_aux.cpp
index 44f3660..8f594e2 100644
--- a/mc_enum_types_aux.cpp
+++ b/mc_enum_types_aux.cpp
@@ -28,7 +28,6 @@
#include "mc_enum.hpp"
#include "mc_enum_types.hpp"
-#include <cstddef> // size_t
#include <stdexcept>
namespace
@@ -75,7 +74,7 @@ std::vector<std::string> allowed_strings_emission()
e_emission emission;
constrain_values(emission);
std::vector<std::string> z;
- for(std::size_t j = 0; j < emission.cardinality(); ++j)
+ for(int j = 0; j < emission.cardinality(); ++j)
{
if(emission.is_allowed(j))
{
diff --git a/miscellany.cpp b/miscellany.cpp
index 467c171..8cb4518 100644
--- a/miscellany.cpp
+++ b/miscellany.cpp
@@ -26,6 +26,7 @@
#include "alert.hpp"
#include "assert_lmi.hpp"
#include "math_functions.hpp" // outward_quotient()
+#include "ssize_lmi.hpp"
#include <algorithm> // equal(), max()
#include <cmath> // ceil(), floor()
@@ -156,7 +157,7 @@ int scale_power(int max_power, double min_value, double
max_value)
/// Return the number of newline characters in a string.
-std::size_t count_newlines(std::string const& s)
+int count_newlines(std::string const& s)
{
return std::count(s.begin(), s.end(), '\n');
}
@@ -186,7 +187,7 @@ std::vector<std::string> split_into_lines(std::string
const& s)
// Assume that there is no newline at the end (or beginning) of
// the string: i.e., that all newline delimiters are internal--
// hence "1u + ".
- LMI_ASSERT(lines.size() == 1u + count_newlines(s));
+ LMI_ASSERT(lmi::ssize(lines) == 1 + count_newlines(s));
return lines;
}
diff --git a/miscellany.hpp b/miscellany.hpp
index 4381f8b..4715f7c 100644
--- a/miscellany.hpp
+++ b/miscellany.hpp
@@ -71,7 +71,7 @@ std::string floating_rep(T t)
{
std::ostringstream oss;
oss << std::hex << std::setfill('0');
- std::size_t realsize = sizeof(T);
+ int realsize = static_cast<int>(sizeof(T));
#if defined __GNUC__ && defined LMI_X87
// For gcc with x87, sizeof(long double) == 12, but only
// ten bytes are significant--the other two are padding.
@@ -130,7 +130,7 @@ template<typename T> bool operator<=(minmax<T> m, T t)
{return m.maximum() <= t;
int LMI_SO scale_power(int max_power, double min_value, double max_value);
-std::size_t LMI_SO count_newlines(std::string const&);
+int LMI_SO count_newlines(std::string const&);
std::vector<std::string> LMI_SO split_into_lines(std::string const&);
diff --git a/mvc_controller.cpp b/mvc_controller.cpp
index e85f5bc..727aa8c 100644
--- a/mvc_controller.cpp
+++ b/mvc_controller.cpp
@@ -50,7 +50,6 @@
#include <wx/wupdlock.h>
#include <wx/xrc/xmlres.h>
-#include <cstddef> // size_t
#include <cstring> // strlen()
namespace
@@ -59,7 +58,7 @@ namespace
///
/// A page selection is maintained for each bookcontrol resource.
-std::map<std::string,std::size_t> last_selected_page;
+std::map<std::string,int> last_selected_page;
/// Custom event to trigger a call to SetFocus(). This action requires
/// a custom event because wxFocusEvent does not change focus--it only
@@ -323,7 +322,7 @@ void MvcController::ConditionallyEnableItems
// also be disabled. Even so, the framework might force a
// different radiobutton to be selected.
bool radiobox_enabled = radiobox->IsEnabled();
- for(std::size_t j = 0; j < datum->cardinality(); ++j)
+ for(int j = 0; j < datum->cardinality(); ++j)
{
radiobox->Enable(j, radiobox_enabled && datum->is_allowed(j));
}
@@ -338,7 +337,7 @@ void MvcController::ConditionallyEnableItems
// WX !! Append(wxArrayString const&) "may be much faster"
// according to wx online help, but that seems untrue: its
// implementation just uses a loop.
- for(std::size_t j = 0; j < datum->cardinality(); ++j)
+ for(int j = 0; j < datum->cardinality(); ++j)
{
if(datum->is_allowed(j))
{
diff --git a/numeric_io_traits.hpp b/numeric_io_traits.hpp
index 1a72166..fde8326 100644
--- a/numeric_io_traits.hpp
+++ b/numeric_io_traits.hpp
@@ -137,7 +137,7 @@ struct numeric_conversion_traits
struct Integral{};
template<> struct numeric_conversion_traits<Integral>
{
- static int digits(long int) {return 1;}
+ template<typename T> static int digits(T) {return 1;}
static std::string simplify(std::string const& s) {return s;}
};
diff --git a/test_coding_rules.cpp b/test_coding_rules.cpp
index 8c25812..b313fe8 100644
--- a/test_coding_rules.cpp
+++ b/test_coding_rules.cpp
@@ -25,7 +25,7 @@
#include "handle_exceptions.hpp"
#include "istream_to_string.hpp"
#include "main_common.hpp"
-#include "miscellany.hpp" // lmi_array_size(), split_into_lines()
+#include "miscellany.hpp" // split_into_lines()
#include <boost/filesystem/convenience.hpp> // fs::extension()
#include <boost/filesystem/fstream.hpp>
@@ -33,7 +33,6 @@
#include <boost/filesystem/path.hpp>
#include <algorithm> // is_sorted()
-#include <cstddef> // size_t
#include <ctime>
#include <iomanip>
#include <ios>
@@ -869,7 +868,7 @@ void check_preamble(file const& f)
bool check_reserved_name_exception(std::string const& s)
{
- static char const*const y[] =
+ static std::set<std::string> const z
// Taboo, and therefore uglified here.
{"D""__""W""IN32""__"
,"_""W""IN32"
@@ -977,8 +976,6 @@ bool check_reserved_name_exception(std::string const& s)
,"__XSLT_LIBXSLT_H__"
,"__mp_copymem"
};
- static int const n = lmi_array_size(y);
- static std::set<std::string> const z(y, y + n);
return contains(z, s);
}
@@ -1119,9 +1116,9 @@ class statistics
void print_summary() const;
private:
- std::size_t files_ {0};
- std::size_t lines_ {0};
- std::size_t defects_ {0};
+ int files_ {0};
+ int lines_ {0};
+ int defects_ {0};
};
statistics& statistics::operator+=(statistics const& z)
diff --git a/timer.cpp b/timer.cpp
index 6182a47..423335d 100644
--- a/timer.cpp
+++ b/timer.cpp
@@ -23,6 +23,8 @@
#include "timer.hpp"
+#include "bourn_cast.hpp"
+
#if defined LMI_POSIX
# include <sys/time.h> // gettimeofday()
#elif defined LMI_MSW
@@ -56,12 +58,12 @@
#if defined LMI_POSIX
# include <unistd.h> // sleep()
-void lmi_sleep(unsigned int seconds) {sleep(seconds);}
+void lmi_sleep(int seconds) {sleep(bourn_cast<unsigned int>(seconds));}
#elif defined LMI_MSW
# if !defined LMI_MS_HEADER_INCLUDED
extern "C" void __stdcall Sleep(unsigned int);
# endif // !defined LMI_MS_HEADER_INCLUDED
-void lmi_sleep(unsigned int seconds) {Sleep(1000 * seconds);}
+void lmi_sleep(int seconds) {Sleep(bourn_cast<unsigned int>(1000 * seconds));}
#else // Unknown platform.
# error Unknown platform.
#endif // Unknown platform.
diff --git a/timer.hpp b/timer.hpp
index ab3cd27..5569a05 100644
--- a/timer.hpp
+++ b/timer.hpp
@@ -33,7 +33,7 @@
#include <stdexcept>
#include <string>
-void lmi_sleep(unsigned int seconds);
+void lmi_sleep(int seconds);
/// Why another timer class?
///
diff --git a/vector_test.cpp b/vector_test.cpp
index d0a0686..fcce46d 100644
--- a/vector_test.cpp
+++ b/vector_test.cpp
@@ -30,7 +30,6 @@
#include "timer.hpp"
#include <algorithm>
-#include <cstddef> // size_t
#include <functional>
#include <iomanip>
#include <ios>
@@ -287,8 +286,8 @@ namespace
simple_array0 g_w(g_array_length);
std::valarray<double> g_va_u(g_array_length);
- std::valarray<double> g_va_v(static_cast<std::size_t>(g_array_length));
- std::valarray<double> g_va_w(static_cast<std::size_t>(g_array_length));
+ std::valarray<double> g_va_v(g_array_length);
+ std::valarray<double> g_va_w(g_array_length);
} // Unnamed namespace.
void mete_c()