[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master fea5864 2/2: Prefer modern dialect
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master fea5864 2/2: Prefer modern dialect |
Date: |
Fri, 30 Mar 2018 16:46:05 -0400 (EDT) |
branch: master
commit fea586445727a91df76ccc2eafd36b15c0b972e6
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Prefer modern dialect
It's not necessarily obvious why the non-const std::string::data()
overload must return a contiguous, null-terminated C string. It's
contiguous because C++17 (N4659) [24.3.2.7.1/4] says it returns
| A pointer p such that
| p + i == &operator[](i) for each i in [0, size()]
; and it's null-terminated because that range includes its right
endpoint, and [24.3.2.5/2] operator[size()] returns charT(), which
equals zero.
Before C++17 added the non-const data() overload, some authors had
recommended '&s[0]' instead of 's.data()'. The latter seems better
for the same reason that 's.front()' is better than 's[0]'; moreover,
'&s[0]' has been reported to elicit compiler warnings where '&s[0U]'
does not.
It's worth mentioning that, while
STARTUPINFO startup_info = {};
is fine, these alternatives:
STARTUPINFO startup_info = {'\0'};
STARTUPINFO startup_info = {0};
elicit diagnostics from gcc-7.2.0 with '-Wall -Wextra':
error: missing initializer for member...
[-Werror=missing-field-initializers]
---
system_command_non_wx.cpp | 23 ++++++++++-------------
1 file changed, 10 insertions(+), 13 deletions(-)
diff --git a/system_command_non_wx.cpp b/system_command_non_wx.cpp
index c38ca0b..09098d8 100644
--- a/system_command_non_wx.cpp
+++ b/system_command_non_wx.cpp
@@ -26,15 +26,9 @@
#include "alert.hpp"
#if !defined LMI_MSW
-
-# include <cstdlib>
-
+# include <cstdlib> // system()
#else // defined LMI_MSW
-
# include <windows.h>
-
-# include <cstring>
-
#endif // defined LMI_MSW
namespace
@@ -61,17 +55,21 @@ void concrete_system_command(std::string const&
command_line)
void concrete_system_command(std::string const& command_line)
{
- STARTUPINFO startup_info;
- std::memset(&startup_info, 0, sizeof(STARTUPINFO));
+ STARTUPINFO startup_info = {};
startup_info.cb = sizeof(STARTUPINFO);
PROCESS_INFORMATION process_info;
- char* non_const_cmd_line_copy = new char[1 + command_line.size()];
- std::strcpy(non_const_cmd_line_copy, command_line.c_str());
+ // For 'wine' at least, this argument cannot be const, even though
+ // this authority:
+ // https://blogs.msdn.microsoft.com/oldnewthing/20090601-00/?p=18083
+ // says that requirement affects "only the Unicode version". It
+ // would seem wrong to change this wrapper's argument type (for
+ // POSIX too) because of this msw implementation detail.
+ std::string non_const_cmd_line_copy = command_line;
::CreateProcessA
(0
- ,non_const_cmd_line_copy
+ ,non_const_cmd_line_copy.data()
,0
,0
,true
@@ -81,7 +79,6 @@ void concrete_system_command(std::string const& command_line)
,&startup_info
,&process_info
);
- delete[]non_const_cmd_line_copy;
DWORD exit_code = 12345;
::CloseHandle(process_info.hThread);