language-lawyer

Why there is no compiler error when return statement is not present?

回眸只為那壹抹淺笑 提交于 2021-01-18 07:46:05
问题 Unlike Java, in C/C++ following is allowed : int* foo () { if(x) return p; // what if control reaches here } This often causes crashes and hard to debug problems. Why standard doesn't enforce to have final return for non- void functions ? (Compilers generate error for wrong return value) Is there any flag in gcc/msvc to enforce this ? (something like -Wunused-result ) 回答1: It is not allowed (undefined behaviour). However, the standard does not require a diagnostic in this case. The standard

Is a nested alias template that redeclares the same name valid?

给你一囗甜甜゛ 提交于 2021-01-07 03:11:18
问题 Given some class template: template <typename> struct T {}; a type alias declaration that changes the meaning of the name T like this is not allowed: struct S { using T = T<void>; // ill formed no diagnostic required }; There is an explanation given here as to why this is the case, and it also suggests a fix: struct S { using T = ::T<void>; // ok }; So my question is, how about an alias template as in this case? struct S { template <typename> using T = T<void>; // ok ? }; Is this well formed?

Is a nested alias template that redeclares the same name valid?

只谈情不闲聊 提交于 2021-01-07 03:10:19
问题 Given some class template: template <typename> struct T {}; a type alias declaration that changes the meaning of the name T like this is not allowed: struct S { using T = T<void>; // ill formed no diagnostic required }; There is an explanation given here as to why this is the case, and it also suggests a fix: struct S { using T = ::T<void>; // ok }; So my question is, how about an alias template as in this case? struct S { template <typename> using T = T<void>; // ok ? }; Is this well formed?

Can the qsort comparison function always return a non-zero value?

牧云@^-^@ 提交于 2021-01-07 01:41:33
问题 An ascending sort callback function for qsort and bsearch on an array of int could look like this: int ascending(const void *o1, const void *o2) { int a = *(const int *)o1; int b = *(const int *)o2; return a < b ? -1 : 1; } Yet this function seems to violate the constraint on the compar function as specified in the C Standard: 7.22.5.2 The qsort function Synopsis #include <stdlib.h> void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); Description The

Can the qsort comparison function always return a non-zero value?

我与影子孤独终老i 提交于 2021-01-07 01:40:48
问题 An ascending sort callback function for qsort and bsearch on an array of int could look like this: int ascending(const void *o1, const void *o2) { int a = *(const int *)o1; int b = *(const int *)o2; return a < b ? -1 : 1; } Yet this function seems to violate the constraint on the compar function as specified in the C Standard: 7.22.5.2 The qsort function Synopsis #include <stdlib.h> void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); Description The

Can the qsort comparison function always return a non-zero value?

你离开我真会死。 提交于 2021-01-07 01:40:47
问题 An ascending sort callback function for qsort and bsearch on an array of int could look like this: int ascending(const void *o1, const void *o2) { int a = *(const int *)o1; int b = *(const int *)o2; return a < b ? -1 : 1; } Yet this function seems to violate the constraint on the compar function as specified in the C Standard: 7.22.5.2 The qsort function Synopsis #include <stdlib.h> void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); Description The

Can the qsort comparison function always return a non-zero value?

拥有回忆 提交于 2021-01-07 01:39:47
问题 An ascending sort callback function for qsort and bsearch on an array of int could look like this: int ascending(const void *o1, const void *o2) { int a = *(const int *)o1; int b = *(const int *)o2; return a < b ? -1 : 1; } Yet this function seems to violate the constraint on the compar function as specified in the C Standard: 7.22.5.2 The qsort function Synopsis #include <stdlib.h> void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); Description The

Can the qsort comparison function always return a non-zero value?

空扰寡人 提交于 2021-01-07 01:39:38
问题 An ascending sort callback function for qsort and bsearch on an array of int could look like this: int ascending(const void *o1, const void *o2) { int a = *(const int *)o1; int b = *(const int *)o2; return a < b ? -1 : 1; } Yet this function seems to violate the constraint on the compar function as specified in the C Standard: 7.22.5.2 The qsort function Synopsis #include <stdlib.h> void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); Description The

Why the Note 3 in [class.temporary]/2? Shouldn't paragraphs (2.1) thru (2.6) have normative status?

微笑、不失礼 提交于 2021-01-07 01:21:39
问题 [class.temporary]/2 The materialization of a temporary object is generally delayed as long as possible in order to avoid creating unnecessary temporary objects. [ Note 3 : Temporary objects are materialized: (2.1) when binding a reference to a prvalue ([dcl.init.ref], [expr.type.conv], [expr.dynamic.cast], [expr.static.cast], [expr.const.cast], [expr.cast]), (2.2) when performing member access on a class prvalue ([expr.ref], [expr.mptr.oper]), (2.3) when performing an array-to-pointer

Why the Note 3 in [class.temporary]/2? Shouldn't paragraphs (2.1) thru (2.6) have normative status?

☆樱花仙子☆ 提交于 2021-01-07 01:18:10
问题 [class.temporary]/2 The materialization of a temporary object is generally delayed as long as possible in order to avoid creating unnecessary temporary objects. [ Note 3 : Temporary objects are materialized: (2.1) when binding a reference to a prvalue ([dcl.init.ref], [expr.type.conv], [expr.dynamic.cast], [expr.static.cast], [expr.const.cast], [expr.cast]), (2.2) when performing member access on a class prvalue ([expr.ref], [expr.mptr.oper]), (2.3) when performing an array-to-pointer