portability

Is std::container::size_type guaranteed to be size_t for standard containers with default allocator?

对着背影说爱祢 提交于 2020-01-01 09:35:06
问题 Like: std::string<T>::size_type std::list<T>::size_type std::map<T>::size_type std::vector<T>::size_type etc. Both cplusplus.com and cppreference.com say that they are usually size_t , but are they truly, unambiguously guaranteed by the standard to be size_t unless a custom allocator is used? 回答1: For STL-containers - nope. Table 96 of the standard in [container.requirements.general], which lists container requirements for any container X , explains it pretty clear: However, for basic_string

In C how to write whichever end of line character is appropriate to the OS?

隐身守侯 提交于 2020-01-01 05:15:10
问题 Unix has \n, Mac was \r but is now \n and DOS/Win32 is \r\n. When creating a text file with C, how to ensure whichever end of line character(s) is appropriate to the OS gets used? 回答1: fprintf(your_file, "\n"); This will be converted to an appropriate EOL by the stdio library on your operating system provided that you opened the file in text mode. In binary mode no conversion takes place. 回答2: From Wikipedia: When writing a file in text mode, '\n' is transparently translated to the native

Cross-platform primitive data types in C++

前提是你 提交于 2020-01-01 04:33:05
问题 Unlike Java or C#, primitive data types in C++ can vary in size depending on the platform. For example, int is not guaranteed to be a 32-bit integer. Various compiler environments define data types such as uint32 or dword for this purpose, but there seems to be no standard include file for fixed-size data types. What is the recommended method to achieve maximum portability? 回答1: I found this header particularly useful: BOOST cstdint Usually better than inventing own wheel (which incurs the

Protocol buffers and UTF-8

人盡茶涼 提交于 2020-01-01 03:39:15
问题 The history of Encoding Schemes / multiple Operating Systems and Endian-nes have led to a mess in terms of encoding all forms of string data (--i.e., all alphabets); for this reason protocol buffers only deals with ASCII or UTF-8 in its string types, and I can't see any polymorphic overloads that accept the C++ wstring. The question then is how is one expected to get a UTF-16 string into a protocol buffer ? Presumably I need to keep the data as a wstring in my application code and then

What do I need to run PHP applications on IIS?

為{幸葍}努か 提交于 2020-01-01 03:31:06
问题 Having been a PHP developer on LAMP servers for quite a while, is there anything that I will need to take into consideration while preparing an application for IIS on windows. 回答1: Make sure you get the FastCGI extension for IIS 6.0 or IIS 7.0. It is the single most important thing you can have when running PHP under IIS. Also this article should get you setup: http://learn.iis.net/page.aspx/247/using-fastcgi-to-host-php-applications-on-iis-60/ Everything beyond this is simple, MySQL and what

How does one include TR1?

蹲街弑〆低调 提交于 2019-12-31 11:27:52
问题 Different compilers seem to have different ideas about TR1. G++ only seems to accept includes of the type: #include <tr1/unordered_map> #include <tr1/memory> ... While Microsofts compiler only accept: #include <unordered_map> #include <memory> ... As for as I understand TR1, the Microsoft way is the correct one. Is there a way to get G++ to accept the second version? How does one in general handle TR1 in a portable way? 回答1: Install boost on your machine. Add the following directory to your

How does one programmatically determine if “write” system call is atomic on a particular file?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-31 00:45:18
问题 In some cases the coder cannot rely on system calls being atomic, e.g. if the file is on a NFS filesystem. (c.f. NFS Overview, FAQ and HOWTO Documents). But atomic system calls are ultimately required for most database work. (c.f. Atomicity of database systems). Is there a standard (and OS independent) way of confirming writes (and other syscalls) are atomic on a particular FILE in C (or python). Any suggestions? Subsequent notes: Atomicity on pipes is discussed in the following: unix pipe

How to get file modification time in c under multiple OS?

旧城冷巷雨未停 提交于 2019-12-30 07:14:10
问题 I'm trying to write a portable function in c that compares the last modified times of 2 files. The files are tiny and written one right after the other, so I need finer granularity than 1 second (milliseconds). There seems to be a plethora of time/date functions... 回答1: The C standard does not have any functions for this, but the Posix specification does. The 2008 edition even provides sub-second timestamps. #define _POSIX_C_SOURCE 200809L The following code should give you an idea how to use

Portability of using stddef.h's offsetof rather than rolling your own

送分小仙女□ 提交于 2019-12-30 06:05:56
问题 This is a nitpicky-details question with three parts. The context is that I wish to persuade some folks that it is safe to use <stddef.h> 's definition of offsetof unconditionally rather than (under some circumstances) rolling their own. The program in question is written entirely in plain old C, so please ignore C++ entirely when answering. Part 1: When used in the same manner as the standard offsetof , does the expansion of this macro provoke undefined behavior per C89, why or why not, and

How do I get the current user in Perl in a portable way?

本秂侑毒 提交于 2019-12-30 05:55:08
问题 How does one get the current user in a portable way? This seems like an FAQ but perlport doesn't speak about it, maybe because some odd systems don't have the concept of "user" to being with? However, let's stick to *nix and Windows. getpwuid($>) is not implemented on Windows. $ENV{USER} || $ENV{USERNAME} seems finicky. http://search.cpan.org didn't turn up much. 回答1: getlogin: This implements the C library function of the same name, which on most systems returns the current login from /etc