language-lawyer

Effective type rules with relation to strict aliasing

徘徊边缘 提交于 2020-05-15 09:32:25
问题 So, I've been banging my head against the Strict Aliasing Rule and the effective type rules for the past couple of days. While the spirit of it is pretty clear, I'd like to nail down a good technical understanding of the rules. Please note I've gone through many related questions on SO, but I don't feel that the questions to be presented here have been answered in a way that really sits with me in any other place. This question is divided into two parts. In the first part, I divide the

Why do c and c++ treat redefinitions of uninitialised variables differently?

本小妞迷上赌 提交于 2020-05-15 02:57:06
问题 int a; int a=3; //error as cpp compiled with clang++-7 compiler but not as C compiled with clang-7; int main() { } For C, the compiler seems to merge these symbols into one global symbol but for C++ it is an error. Demo file1: int a = 2; file2: #include<stdio.h> int a; int main() { printf("%d", a); //2 } As C files compiled with clang-7, the linker does not produce an error and I assume it converts the uninitialised global symbol 'a' to an extern symbol (treating it as if it were compiled as

Where are the statement of or the foundations for the “as if” rule in the C++ Standard?

試著忘記壹切 提交于 2020-05-15 02:42:04
问题 After a little google search (for instance, site:eel.is "as if rule" ) I couldn't find a proper place where the so called "as if" rule is clearly stated in the C++ standard. All I could find is that in those places within the standard where it is invoked, the intro.execution reference is given. But intro.execution doen't seem to clearly reference any general form of this rule. I'm probably missing something subtle here, but can you point me to the place, or places, where a precise normative

Is calculating address difference undefined behaviour?

对着背影说爱祢 提交于 2020-05-15 02:10:46
问题 Let's say I perform the following: void g(int* x) { int y = 0; auto diff = uintptr_t(&y) - uintptr_t(x); } void f() { int x = 0; g(&x); } Does diff merely have undefined value, or does the code invoke undefined behaviour? According to the specification, is the code guaranteed to run nicely and compute a value for diff , possibly meaningless, or does it invoke UB? I believe there's something about unrelated variables, but could not pinpoint it. I'm interested in answers regarding any standard

Is calculating address difference undefined behaviour?

ぐ巨炮叔叔 提交于 2020-05-15 02:08:59
问题 Let's say I perform the following: void g(int* x) { int y = 0; auto diff = uintptr_t(&y) - uintptr_t(x); } void f() { int x = 0; g(&x); } Does diff merely have undefined value, or does the code invoke undefined behaviour? According to the specification, is the code guaranteed to run nicely and compute a value for diff , possibly meaningless, or does it invoke UB? I believe there's something about unrelated variables, but could not pinpoint it. I'm interested in answers regarding any standard

Is calculating address difference undefined behaviour?

匆匆过客 提交于 2020-05-15 02:07:15
问题 Let's say I perform the following: void g(int* x) { int y = 0; auto diff = uintptr_t(&y) - uintptr_t(x); } void f() { int x = 0; g(&x); } Does diff merely have undefined value, or does the code invoke undefined behaviour? According to the specification, is the code guaranteed to run nicely and compute a value for diff , possibly meaningless, or does it invoke UB? I believe there's something about unrelated variables, but could not pinpoint it. I'm interested in answers regarding any standard

Construct standard exceptions with null pointer argument and impossible postconditions

感情迁移 提交于 2020-05-14 16:43:11
问题 Consider the following program: #include<stdexcept> #include<iostream> int main() { try { throw std::range_error(nullptr); } catch(const std::range_error&) { std::cout << "Caught!\n"; } } GCC and Clang with libstdc++ call std::terminate and abort the program with the message terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid Clang with libc++ segfaults on construction of the exception. See godbolt. Are the compilers behaving

Is missing a required include undefined behavior?

六月ゝ 毕业季﹏ 提交于 2020-05-13 05:29:31
问题 As I wrote an answer to How is it possible to use pow without including cmath library I fear to have proven that missing an include of a needed header is actually undefined behavior, but since I have not found any consent of that fact I like to impose the formal question: Is missing a required header i.e. #include <iostream> int main() { std::cout << std::pow(10, 2); } Ill-formed ( [defns.ill.formed] ) code? Invoking undefined behavior ( [defns.undefined] )? If it is not 1 and 2, is it

Is missing a required include undefined behavior?

给你一囗甜甜゛ 提交于 2020-05-13 05:29:10
问题 As I wrote an answer to How is it possible to use pow without including cmath library I fear to have proven that missing an include of a needed header is actually undefined behavior, but since I have not found any consent of that fact I like to impose the formal question: Is missing a required header i.e. #include <iostream> int main() { std::cout << std::pow(10, 2); } Ill-formed ( [defns.ill.formed] ) code? Invoking undefined behavior ( [defns.undefined] )? If it is not 1 and 2, is it

Alias of a template. Who's right?

回眸只為那壹抹淺笑 提交于 2020-05-13 04:47:46
问题 The following code seems reasonable, but doesn't work on two major compilers #include <type_traits> template<template<class> class Tmp> struct S{ template<class T> using tmp_t = Tmp<T>; static_assert(std::is_same_v< S<tmp_t>, S<Tmp> >, "Not same?? How come?"); }; template<class> struct Dummy{}; template struct S<Dummy>; gcc starting with 7.1 compiles alright ( https://godbolt.org/z/DjAcgP ) clang ( https://godbolt.org/z/ewBbZJ ) and msvc ( https://godbolt.org/z/6ZmQwj ) fail to do so Is this