Jean-Marc Lasgouttes wrote:
> Le 07/03/2016 13:21, Alex Vergara Gil a écrit :
>> Best way for this is to use native int as integer variables and you
>> forget about it, this works even on windows, so instead of int use
>> long and thats it: http://en.cppreference.com/w/cpp/language/types
>
> In this case one would want a 64 bit type, and long is 32bits in win64.
We do not always want 64bit types. For each of the warnings, we need to
decide which of the following cases applies:
- The wanted type is always 64bit => use boost::int64_t for signed values
and boost::uint64_t for unsigned values (MSVC does not have std::int64_t or
::it64_t, at least not in the 2010 version)
- The wanted type is always 32bit => use int for signed values and
boost::uint32_t for unsigned values
- A pointer needs to fit into the wanted type => use ptrdiff_t for signed
values and size_t for unsigned values
This may indeed involve some ugly casts (like the second part of int1.diff),
or more involved changes. See e.g. AuthorList::record(): The computed value
is of type ptrdiff_t, but we do not want this to propagate outside the
method, so we either need a static_cast<int>, or a separate counter (like in
the first part in int1.diff). Either solution could be implemented in a safe
way even now, but in the long term one would probably want to change the id
type to size_t (like in int2.diff), but unless int1.diff which can be
verified to be correct by only looking at the code in AuthorList::record(),
int2.diff would need more work to verify that it does not introduce a
regression.
Georg
diff --git a/src/Author.cpp b/src/Author.cpp
index bd9d12d..55d5ca9 100644
--- a/src/Author.cpp
+++ b/src/Author.cpp
@@ -18,6 +18,7 @@
#include <algorithm>
#include <istream>
+#include <limits>
using namespace std;
using namespace lyx::support;
@@ -103,15 +104,13 @@ int AuthorList::record(Author const & a)
if (valid && !authors_.empty() && a == authors_[0])
authors_[0].setBufferId(a.bufferId());
- Authors::const_iterator it = authors_.begin();
- Authors::const_iterator const beg = it;
- Authors::const_iterator const end = authors_.end();
- for (; it != end; ++it) {
- if (valid && *it == a)
- return it - beg;
- if (it->bufferId() == a.bufferId()) {
- int id = it - beg;
- if (!it->valid())
+ LBUFERR(authors_.size() < static_cast<size_t>(numeric_limits<int>::max()));
+ int const n = static_cast<int>(authors_.size());
+ for (int id = 0; id < n; ++id) {
+ if (valid && authors_[id] == a)
+ return id;
+ if (authors_[id].bufferId() == a.bufferId()) {
+ if (!authors_[id].valid())
// we need to handle the case of a valid author being registred
// after an invalid one. For instance, because "buffer-reload"
// does not clear the buffer's author list.
diff --git a/src/support/os_win32.cpp b/src/support/os_win32.cpp
index c50fa05..7ad544c 100644
--- a/src/support/os_win32.cpp
+++ b/src/support/os_win32.cpp
@@ -563,7 +563,7 @@ bool autoOpenFile(string const & filename, auto_open_mode const mode,
// reference: http://msdn.microsoft.com/en-us/library/bb762153.aspx
char const * action = (mode == VIEW) ? "open" : "edit";
- bool success = reinterpret_cast<int>(ShellExecute(NULL, action,
+ bool success = reinterpret_cast<ptrdiff_t>(ShellExecute(NULL, action,
to_local8bit(from_utf8(filename)).c_str(), NULL, NULL, 1)) > 32;
if (!path.empty() && !lyxrc.texinputs_prefix.empty()) {
diff --git a/src/Author.cpp b/src/Author.cpp
index bd9d12d..e52505a 100644
--- a/src/Author.cpp
+++ b/src/Author.cpp
@@ -94,7 +94,7 @@ AuthorList::AuthorList()
{}
-int AuthorList::record(Author const & a)
+size_t AuthorList::record(Author const & a)
{
bool const valid = a.valid();
// If we record an author which equals the current
@@ -110,13 +110,12 @@ int AuthorList::record(Author const & a)
if (valid && *it == a)
return it - beg;
if (it->bufferId() == a.bufferId()) {
- int id = it - beg;
if (!it->valid())
// we need to handle the case of a valid author being registred
// after an invalid one. For instance, because "buffer-reload"
// does not clear the buffer's author list.
- record(id, a);
- return id;
+ record(it - beg, a);
+ return it - beg;
}
}
authors_.push_back(a);
@@ -124,9 +123,9 @@ int AuthorList::record(Author const & a)
}
-void AuthorList::record(int id, Author const & a)
+void AuthorList::record(size_t id, Author const & a)
{
- LBUFERR(unsigned(id) < authors_.size());
+ LBUFERR(id < authors_.size());
authors_[id] = a;
}
@@ -138,9 +137,9 @@ void AuthorList::recordCurrentAuthor(Author const & a)
}
-Author const & AuthorList::get(int id) const
+Author const & AuthorList::get(size_t id) const
{
- LASSERT(id < (int)authors_.size() , return authors_[0]);
+ LASSERT(id < authors_.size(), return authors_[0]);
return authors_[id];
}
diff --git a/src/Author.h b/src/Author.h
index 6915318..41955af 100644
--- a/src/Author.h
+++ b/src/Author.h
@@ -63,13 +63,13 @@ public:
///
AuthorList();
///
- int record(Author const & a);
+ size_t record(Author const & a);
///
- void record(int id, Author const & a);
+ void record(size_t id, Author const & a);
///
void recordCurrentAuthor(Author const & a);
///
- Author const & get(int id) const;
+ Author const & get(size_t id) const;
///
void sort();
///