language-lawyer

Standard behavior for direct initialization of unsigned short

牧云@^-^@ 提交于 2019-12-23 06:54:47
问题 I noticed today that in the example code: void print(unsigned short a) { std::cout << a << std::endl; } Initialization and use works like this: print(short (5)); But not like this: print(unsigned short(6)); main.cpp:16:8: error: expected primary-expression before 'unsigned' print(unsigned short(6)); And it's not to do with the type since this also works: typedef unsigned short ushort; print(ushort (6)); Live example. So I went searching for what the standard says about value initialization.

C operator += Sequence point?

喜欢而已 提交于 2019-12-23 05:33:09
问题 Is this defined behaviour? *p += *p--; And, if it is, is it equivalent to { p[0] += p[0]; --p; } or to { p[-1] = p[0]; --p; } ? I'm guessing the being defined or not depends on whether += has an implicit sequence point and, if it has, my guess is that the second block should be the correct one. EDIT: I think it's not a duplicate of the suggested question because the main question there is what are sequence points and how do the affect behaviour. In my case I have clear idea of what a sequence

When specializing a class, how can I take a different number of template parameters?

不羁岁月 提交于 2019-12-23 04:09:22
问题 I just asked this question: Can I get the Owning Object of a Member Function Template Parameter? and Yakk - Adam Nevraumont's answer had the code: template<class T> struct get_memfun_class; template<class R, class T, class...Args> struct get_memfun_class<R(T::*)(Args...)> { using type=T; }; These is clearly an initial declaration and then a specialization of struct get_memfun_class . But I find myself uncertain: Can specializations have a different number of template parameters? For example,

CPUs with addressable GPR files, address of register variables, and aliasing between memory and registers [closed]

梦想与她 提交于 2019-12-23 02:36:14
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . Background Some CPUs, such as the Atmel AVR, have a general purpose register file that is also addressable as part of main memory -- see Figure 7-2 in section 7.4 and the paragraph after the figure. What was WG14 thinking? Given this, why did the C committee choose to make

CPUs with addressable GPR files, address of register variables, and aliasing between memory and registers [closed]

家住魔仙堡 提交于 2019-12-23 02:36:11
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . Background Some CPUs, such as the Atmel AVR, have a general purpose register file that is also addressable as part of main memory -- see Figure 7-2 in section 7.4 and the paragraph after the figure. What was WG14 thinking? Given this, why did the C committee choose to make

Where is the standard wording allowing incomplete types in function declarations, but requiring complete types in function definitions?

梦想与她 提交于 2019-12-23 02:34:32
问题 I recently found out that the types of parameters in a non-defining function declaration may be of incomplete types. This is very exciting. class A; class B { B(A a); // Legal! Wow! }; The type is required to be complete only for the definition: B::B(A a) {}; // error: ‘a’ has incomplete type I've been trying to pin down the legalese for this, but my searches through C++11 for "[in]complete type" have yielded nothing of much interest, leading me to assume that these semantics are defined

Is it undefined behavior if the destination string in strcat function is not null terminated?

元气小坏坏 提交于 2019-12-23 01:36:19
问题 The following program // Code has taken from http://ideone.com/AXClWb #include <stdio.h> #include <string.h> #define SIZE1 5 #define SIZE2 10 #define SIZE3 15 int main(void){ char a[SIZE1] = "Hello"; char b[SIZE2] = " World"; char res[SIZE3] = {0}; for (int i=0 ; i<SIZE1 ; i++){ res[i] = a[i]; } strcat(res, b); printf("The new string is: %s\n",res); return 0; } has well defined behavior. As per the requirement, source string b is null terminated. But what would be the behavior if the line

How are array and pointer types handled internally in C compilers? ( int *a; vs. int a[]; )

对着背影说爱祢 提交于 2019-12-22 18:23:50
问题 I need a language lawyer with authoritative sources. Take a look at the following test program which compiles cleanly under gcc: #include <stdio.h> void foo(int *a) { a[98] = 0xFEADFACE; } void bar(int b[]) { *(b+498) = 0xFEADFACE; } int main(int argc, char **argv) { int a[100], b[500], *a_p; *(a+99) = 0xDEADBEEF; *(b+499) = *(a+99); foo(a); bar(b); printf("a[98] == %X\na[99] == %X\n", a[98], a[99]); printf("b[498] == %X\nb[499] == %X\n", b[498], b[499]); a_p = a+98; *a_p = 0xDEADFACE; printf

Is the compiler allowed to optimize out heap memory allocations?

情到浓时终转凉″ 提交于 2019-12-22 13:58:58
问题 Consider the following simple code that makes use of new (I am aware there is no delete[] , but it does not pertain to this question): int main() { int* mem = new int[100]; return 0; } Is the compiler allowed to optimize out the new call? In my research, g++ (5.2.0) and Visual Studio 2015 do not optimize out the new call, while clang (3.0+) does. All tests have been made with full optimizations enabled (-O3 for g++ and clang, Release mode for Visual Studio). Isn't new making a system call

Lower bound for the maximum level of ownership for recursive_mutex?

拟墨画扇 提交于 2019-12-22 13:09:31
问题 Quoting [thread.mutex.recursive]: A thread that owns a recursive_mutex object may acquire additional levels of ownership by calling lock() or try_lock() on that object. It is unspecified how many levels of ownership may be acquired by a single thread. If a thread has already acquired the maximum level of ownership for a recursive_mutex object, additional calls to try_lock() shall fail, and additional calls to lock() shall throw an exception of type system_error . Is there a lower bound