{"action":"created","issue":{"url":"https://api.github.com/repos/ericniebler/range-v3/issues/239","labels_url":"https://api.github.com/repos/ericniebler/range-v3/issues/239/labels{/name}","comments_url":"https://api.github.com/repos/ericniebler/range-v3/issues/239/comments","events_url":"https://api.github.com/repos/ericniebler/range-v3/issues/239/events","html_url":"https://github.com/ericniebler/range-v3/issues/239","id":116407087,"number":239,"title":"Undefined behavior in sized_range","user":{"login":"CaseyCarter","id":456873,"avatar_url":"https://avatars.githubusercontent.com/u/456873?v=3","gravatar_id":"","url":"https://api.github.com/users/CaseyCarter","html_url":"https://github.com/CaseyCarter","followers_url":"https://api.github.com/users/CaseyCarter/followers","following_url":"https://api.github.com/users/CaseyCarter/following{/other_user}","gists_url":"https://api.github.com/users/CaseyCarter/gists{/gist_id}","starred_url":"https://api.github.com/users/CaseyCarter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CaseyCarter/subscriptions","organizations_url":"https://api.github.com/users/CaseyCarter/orgs","repos_url":"https://api.github.com/users/CaseyCarter/repos","events_url":"https://api.github.com/users/CaseyCarter/events{/privacy}","received_events_url":"https://api.github.com/users/CaseyCarter/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-11-11T19:43:05Z","updated_at":"2015-12-18T17:45:29Z","closed_at":null,"body":"`sized_range` as defined in range.hpp uses a clever technique to defend its class invariant despite exposing public data members: the data members are declared `const`, and mutating operations cast away member constness. This is formally undefined behavior per [dcl.type.cv]/4.\r\n"},"comment":{"url":"https://api.github.com/repos/ericniebler/range-v3/issues/comments/165850625","html_url":"https://github.com/ericniebler/range-v3/issues/239#issuecomment-165850625","issue_url":"https://api.github.com/repos/ericniebler/range-v3/issues/239","id":165850625,"user":{"login":"gnzlbg","id":904614,"avatar_url":"https://avatars.githubusercontent.com/u/904614?v=3","gravatar_id":"","url":"https://api.github.com/users/gnzlbg","html_url":"https://github.com/gnzlbg","followers_url":"https://api.github.com/users/gnzlbg/followers","following_url":"https://api.github.com/users/gnzlbg/following{/other_user}","gists_url":"https://api.github.com/users/gnzlbg/gists{/gist_id}","starred_url":"https://api.github.com/users/gnzlbg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gnzlbg/subscriptions","organizations_url":"https://api.github.com/users/gnzlbg/orgs","repos_url":"https://api.github.com/users/gnzlbg/repos","events_url":"https://api.github.com/users/gnzlbg/events{/privacy}","received_events_url":"https://api.github.com/users/gnzlbg/received_events","type":"User","site_admin":false},"created_at":"2015-12-18T17:45:29Z","updated_at":"2015-12-18T17:45:29Z","body":"IIUC what @CaseyCarter points out is that UB results from doing something like:\r\n\r\n```c++\r\n++const_cast<int&>(f.first); // UB because first is const\r\n```\r\n\r\n2. Aggregate initialization of the union is initializing the `first_` data member. In your example, `first` is thus not active, and reading from it is UB as well. "}}