language-lawyer

The difference between declaring a name, introducing a name, and declaring an entity

吃可爱长大的小学妹 提交于 2021-02-18 22:30:28
问题 From the C++11 standard, §7.3.3[namespace.udecl]/1: A using-declaration introduces a name into the declarative region in which the using-declaration appears. using-declaration : using typename opt nested-name-specifier unqualified-id ; using :: unqualified-id ; The member name specified in a using-declaration is declared in the declarative region in which the using-declaration appears. What do they mean by the name being declared in the declarative region where the using-declaration occurs?

Casting to void (not pointer) is allowed, why?

这一生的挚爱 提交于 2021-02-18 20:15:15
问题 Why can I cast this vector to a void (not even a pointer)? int main() { std::vector<int> a; (void)a; } How come this is allowed? 回答1: Casting to void simply discards the result of the expression. Sometimes, you'll see people using this to avoid "unused variable" or "ignored return value" warnings. In C++, you should probably write static_cast<void>(expr); rather than (void)expr; This has the same effect of discarding the value, while making it clear what kind of conversion is being performed.

Casting to void (not pointer) is allowed, why?

倖福魔咒の 提交于 2021-02-18 20:15:11
问题 Why can I cast this vector to a void (not even a pointer)? int main() { std::vector<int> a; (void)a; } How come this is allowed? 回答1: Casting to void simply discards the result of the expression. Sometimes, you'll see people using this to avoid "unused variable" or "ignored return value" warnings. In C++, you should probably write static_cast<void>(expr); rather than (void)expr; This has the same effect of discarding the value, while making it clear what kind of conversion is being performed.

Assign volatile to non-volatile sematics and the C standard

浪子不回头ぞ 提交于 2021-02-18 16:14:29
问题 volatile int vfoo = 0; void func() { int bar; do { bar = vfoo; // L.7 }while(bar!=1); return; } This code busy-waits for the variable to turn to 1 . If on first pass vfoo is not set to 1 , will I get stuck inside. This code compiles without warning. What does the standard say about this? vfoo is declared as volatile . Therefore, read to this variable should not be optimized. However, bar is not volatile qualified. Is the compiler allowed to optimize the write to this bar ? .i.e. the compiler

Assign volatile to non-volatile sematics and the C standard

百般思念 提交于 2021-02-18 16:06:43
问题 volatile int vfoo = 0; void func() { int bar; do { bar = vfoo; // L.7 }while(bar!=1); return; } This code busy-waits for the variable to turn to 1 . If on first pass vfoo is not set to 1 , will I get stuck inside. This code compiles without warning. What does the standard say about this? vfoo is declared as volatile . Therefore, read to this variable should not be optimized. However, bar is not volatile qualified. Is the compiler allowed to optimize the write to this bar ? .i.e. the compiler

Assign volatile to non-volatile sematics and the C standard

拥有回忆 提交于 2021-02-18 16:06:32
问题 volatile int vfoo = 0; void func() { int bar; do { bar = vfoo; // L.7 }while(bar!=1); return; } This code busy-waits for the variable to turn to 1 . If on first pass vfoo is not set to 1 , will I get stuck inside. This code compiles without warning. What does the standard say about this? vfoo is declared as volatile . Therefore, read to this variable should not be optimized. However, bar is not volatile qualified. Is the compiler allowed to optimize the write to this bar ? .i.e. the compiler

Overloading operator new with smaller default alignment

左心房为你撑大大i 提交于 2021-02-18 11:18:05
问题 C++17 introduced Dynamic memory allocation for over-aligned data Beside the existing std::max_align_t , the fundamental alignment, it added __STDCPP_DEFAULT_NEW_ALIGNMENT__ the minimal alignment that the operator new guarantees. With MSVC2017 64bit compilation, these constants result in a std::max_align_t of size 8 and __STDCPP_DEFAULT_NEW_ALIGNMENT__ of size 16. It is however allowed to overrule the operator new/free, as mentioned on cppreference: operator new - global replacements. Looking

friend declaration of template specialization fails

爷,独闯天下 提交于 2021-02-18 11:16:48
问题 The following code containing friend declaration fails with indicated error (see http://ideone.com/Kq5dy): template<class T> void foo() {} template<typename T> class A { void foo(); friend void foo<T>(); // error: variable or field 'foo' declared void }; int main() { foo<int>(); } If the order of friend declaration and member function declaration reversed, then the code compiles without problems (see http://ideone.com/y3hiK): template<class T> void foo() {} template<typename T> class A {

overload resolution between lvalue reference and rvalue reference

只愿长相守 提交于 2021-02-18 09:56:26
问题 #include <iostream> using namespace std; void func(int (&ref)[6]) { cout << "#1" << endl; } void func(int * &&ref) { cout << "#2" << endl; } int main() { int arr[6]; func(arr); // g++(5.4): ambiguous, clang++(3.8): #2, vc++(19.11): #1 return 0; } Both functions are exact matches. Below is a quote from the standard: Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if ... S1 and S2 are reference bindings (8.5.3) and neither refers to an

overload resolution between lvalue reference and rvalue reference

旧巷老猫 提交于 2021-02-18 09:55:50
问题 #include <iostream> using namespace std; void func(int (&ref)[6]) { cout << "#1" << endl; } void func(int * &&ref) { cout << "#2" << endl; } int main() { int arr[6]; func(arr); // g++(5.4): ambiguous, clang++(3.8): #2, vc++(19.11): #1 return 0; } Both functions are exact matches. Below is a quote from the standard: Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if ... S1 and S2 are reference bindings (8.5.3) and neither refers to an