language-lawyer

GCC throws init-list-lifetime warning on potentially valid code?

守給你的承諾、 提交于 2021-02-20 10:16:29
问题 I'm running on Debian unstable with GCC 9.3.0. There was a recent change on a project I work on that introduced code similar to what's below. #include <initializer_list> #include <map> #include <vector> std::map<int, std::vector<int>> ex = []{ /* for reused lists */ std::initializer_list<int> module_options; return (decltype(ex)) { {1, module_options = { 1, 2, 3 }}, {2, module_options}, }; }(); The idea is that identical subsections of the initializer lists are first declared at the top,

GCC throws init-list-lifetime warning on potentially valid code?

故事扮演 提交于 2021-02-20 10:15:59
问题 I'm running on Debian unstable with GCC 9.3.0. There was a recent change on a project I work on that introduced code similar to what's below. #include <initializer_list> #include <map> #include <vector> std::map<int, std::vector<int>> ex = []{ /* for reused lists */ std::initializer_list<int> module_options; return (decltype(ex)) { {1, module_options = { 1, 2, 3 }}, {2, module_options}, }; }(); The idea is that identical subsections of the initializer lists are first declared at the top,

GCC throws init-list-lifetime warning on potentially valid code?

倾然丶 夕夏残阳落幕 提交于 2021-02-20 10:15:10
问题 I'm running on Debian unstable with GCC 9.3.0. There was a recent change on a project I work on that introduced code similar to what's below. #include <initializer_list> #include <map> #include <vector> std::map<int, std::vector<int>> ex = []{ /* for reused lists */ std::initializer_list<int> module_options; return (decltype(ex)) { {1, module_options = { 1, 2, 3 }}, {2, module_options}, }; }(); The idea is that identical subsections of the initializer lists are first declared at the top,

x86 BSWAP instruction REX doesn't follow Intel specs?

风格不统一 提交于 2021-02-20 06:27:43
问题 I've been assembling (and disassembling) the BSWAP x64 instruction with both NASM and GAS, and both assemble the instruction BSWAP r15 as 490FCF in hex. Disassemblers also disassemble this to the same instruction. The REX prefix for the instruction ( 49 ) thus has the REX.W bit (bit 3) and the REX.B bit (bit 0) set. This is directly in contrast to the Intel documentation, which states: In 64-bit mode, the instruction’s default operation size is 32 bits. Using a REX prefix in the form of REX.R

x86 BSWAP instruction REX doesn't follow Intel specs?

≯℡__Kan透↙ 提交于 2021-02-20 06:25:41
问题 I've been assembling (and disassembling) the BSWAP x64 instruction with both NASM and GAS, and both assemble the instruction BSWAP r15 as 490FCF in hex. Disassemblers also disassemble this to the same instruction. The REX prefix for the instruction ( 49 ) thus has the REX.W bit (bit 3) and the REX.B bit (bit 0) set. This is directly in contrast to the Intel documentation, which states: In 64-bit mode, the instruction’s default operation size is 32 bits. Using a REX prefix in the form of REX.R

Is a redeclaration of an untagged structure a compatible type?

十年热恋 提交于 2021-02-20 06:12:43
问题 For purposes expressed in this question, we want to do this: typedef struct { int a; } A; typedef struct { struct { int a; }; int b; } B; A *BToA(B *b) { return (A *) b; } B *AToB(A *a) { return (B *) a; } The desire is that the casts conform to C 2011 6.7.2.1 15, which says “A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa.” Since the struct { int a; } inside B does not

C4297 warning in Visual Studio while using function-try-block (function assumed not to throw an exception but does)

我的梦境 提交于 2021-02-19 03:28:52
问题 #include <exception> struct FOO { ~FOO() try { throw std::exception(); } catch (...) { return; // Shall prevent the exception from being rethrown? } }; Building this code in Visual Studio triggers C4297 warning (function assumed not to throw an exception but does). Reaching the end of a catch clause for a function-try-block on a destructor also automatically rethrows the current exception as if by throw;, but a return statement is allowed . quoted from cppreference.com; Do I interpret this

Javac missing optimization for effective final

百般思念 提交于 2021-02-19 01:36:30
问题 Fact: javac is programmed to detect if a variable is final or if it can be treated as effectively final . Proof: This code illustrates this. public static void finalCheck() { String str1 = "hello"; Runnable r = () -> { str1 = "hello"; }; } This fails to compile because compiler is able to detect String reference str1 is being re-assigned in function. Now Situation 1: Javac does great optimization for final String instances by avoiding to create StringBuilder and related operations. Proof This

Weird behaviour constexpr with std::initializer_list

末鹿安然 提交于 2021-02-18 23:00:34
问题 I am trying to understand why the compiler is complaining here: // cexpr_test.cpp #include <initializer_list> constexpr int test_cexpr(std::initializer_list<const char*> x) { return (int) (*x.begin())[0]; // ensuring the value isn't optimized out. } int main() { constexpr int r1 = test_cexpr({ "why does this work," }); constexpr std::initializer_list<const char*> broken { "but this doesn't?" }; constexpr int r2 = test_cexpr(broken); return r1 + r2; } The message produced when compiled with g+

Weird behaviour constexpr with std::initializer_list

大城市里の小女人 提交于 2021-02-18 23:00:13
问题 I am trying to understand why the compiler is complaining here: // cexpr_test.cpp #include <initializer_list> constexpr int test_cexpr(std::initializer_list<const char*> x) { return (int) (*x.begin())[0]; // ensuring the value isn't optimized out. } int main() { constexpr int r1 = test_cexpr({ "why does this work," }); constexpr std::initializer_list<const char*> broken { "but this doesn't?" }; constexpr int r2 = test_cexpr(broken); return r1 + r2; } The message produced when compiled with g+