portability

Portable Compare And Swap (atomic operations) C/C++ library?

∥☆過路亽.° 提交于 2019-11-26 15:21:06
问题 Is there any small library, that wrapps various processors' CAS-like operations into macros or functions, that are portable across multiple compilers? PS. The atomic.hpp library is inside boost::interprocess::detail namespace. The author refuses to make it a public, well maintained library. Lets reopen the question, and see if there are any other options? 回答1: Intel Threading Building Blocks has a nice portable atomic<T> template which does what you want. But whether it is a small library or

How should I print types like off_t and size_t?

我们两清 提交于 2019-11-26 14:56:45
I'm trying to print types like off_t and size_t . What is the correct placeholder for printf() that is portable ? Or is there a completely different way to print those variables? You can use z for size_t and t for ptrdiff_t like in printf("%zu %td", size, ptrdiff); But my manpage says some older library used a different character than z and discourages use of it. Nevertheless, it's standardized (by the C99 standard). For those intmax_t and int8_t of stdint.h and so on, there are macros you can use, like another answer said: printf("value: %" PRId32, some_int32_t); printf("value: %" PRIu16,

How to determine integer types that are twice the width as `int` and `unsigned`?

。_饼干妹妹 提交于 2019-11-26 14:54:05
问题 Values of intermediate multiplication typically need twice the number of bits as inputs. // Example int foo(int a, int b, int carry, int rem) { int2x c; // Some type that is twice as wide at `int` c = (int2x)a * b + carry; return (int) (c % rem); } Considering the potential for padding, (which appears to limit sizeof() usefulness) and non-2`s complement integers (which limits bit dibbling), ... Does the following always create the needed type? If not, how to code at least a reasonable

Why does C++ parameter scope affect function lookup within a namespace?

我们两清 提交于 2019-11-26 14:41:42
问题 This seems a little backwards to me but it works: #include <iostream> namespace nTest { struct cTest {}; void fTest(cTest& x) { std::cout << "nTest::fTest(cTest&) called" << std::endl; } } int main(void) { nTest::cTest x; fTest(x); //Weird! fTest is resolved since its parameter belongs to nTest. return 0; } Normally, you would need nTest:: in order to access fTest, but its parameter which belongs to nTest appears to add nTest to the list of possible scopes in which to search for fTest. It

Portable end of line (newline)

怎甘沉沦 提交于 2019-11-26 14:38:40
问题 It's been an unpleasant surprise that '\n' is replaced with "\r\n" on Windows, I did not know that. (I am guessing it is also replaced on Mac...) Is there an easy way to ensure that Linux, Mac and Windows users can easily exchange text files? By easy way I mean: without writing the file in binary mode or testing and replacing the end-of-line chars myself (or with some third party program/code). This issue effects my C++ program doing the text file I/O. 回答1: Sorry for partial overlap with

What belongs in an educational tool to demonstrate the unwarranted assumptions people make in C/C++?

爱⌒轻易说出口 提交于 2019-11-26 12:35:39
I'd like to prepare a little educational tool for SO which should help beginners (and intermediate) programmers to recognize and challenge their unwarranted assumptions in C, C++ and their platforms. Examples: "integers wrap around" "everyone has ASCII" "I can store a function pointer in a void*" I figured that a small test program could be run on various platforms, which runs the "plausible" assumptions which are, from our experience in SO, usually made by many inexperienced/semiexperienced mainstream developers and record the ways they break on diverse machines. The goal of this is not to

Can placement new for arrays be used in a portable way?

◇◆丶佛笑我妖孽 提交于 2019-11-26 12:20:59
Is it possible to actually make use of placement new in portable code when using it for arrays? It appears that the pointer you get back from new[] is not always the same as the address you pass in (5.3.4, note 12 in the standard seems to confirm that this is correct), but I don't see how you can allocate a buffer for the array to go in if this is the case. The following example shows the problem. Compiled with Visual Studio, this example results in memory corruption: #include <new> #include <stdio.h> class A { public: A() : data(0) {} virtual ~A() {} int data; }; int main() { const int

How do I detect if I&#39;m running MATLAB or Octave?

最后都变了- 提交于 2019-11-26 11:15:49
问题 I need to write code that should run equally well in Octave and on MATLAB. Problem is that it needs to do some GUI stuff, which MATLAB and Octave handle completely differently. Is there a way I can detect if I\'m running MATLAB or Octave, in order to call the right function? 回答1: You could use the following test to differentiate Octave from MATLAB: isOctave = exist('OCTAVE_VERSION', 'builtin') ~= 0; 回答2: There is also a hint in the wiki on the official octave.org website. They propose the

How to write portable code in c++?

老子叫甜甜 提交于 2019-11-26 10:58:12
问题 What are the things that I should keep in mind to write portable code? Since I\'m a c++ beginner, I want to practice it since beginning. Thanks. 回答1: learn to use the standard library read books (eg. this one) when you're experienced, learn to use boost 回答2: Keep platform-specific code separate from reusable code, preferably in a different file but at least in a different function. If you start having #if WIN32 and #if CYGWIN and #if BSD all over the place you'll have a maintenance nightmare.

Portability of #warning preprocessor directive

99封情书 提交于 2019-11-26 09:56:07
问题 I know that the #warning directive is not standard C /C++, but several compilers support it, including gcc/g++. But for those that don\'t support it, will they silently ignore it or will it result in a compile failure? In other words, can I safely use it in my project without breaking the build for compilers that don\'t support it? 回答1: It is likely that if a compiler doesn't support #warning, then it will issue an error. Unlike #pragma, there is no recommendation that the preprocessor ignore