language-lawyer

Is incrementing a null pointer well-defined?

拈花ヽ惹草 提交于 2020-01-09 04:52:28
问题 There are lots of examples of undefined/unspecified behavior when doing pointer arithmetics - pointers have to point inside the same array (or one past the end), or inside the same object, restrictions on when you can do comparisons/operations based on the above, etc. Is the following operation well-defined? int* p = 0; p++; 回答1: §5.2.6/1: The value of the operand object is modified by adding 1 to it, unless the object is of type bool [..] And additive expressions involving pointers are

Use of typename keyword with typedef and new

本秂侑毒 提交于 2020-01-09 03:27:05
问题 Consider this code, template<class T> struct Sample { typename T::X *x; //declare pointer to T's X }; In the above code, the keyword typename is required by the compiler, so that it can disambiguate between nested types and nested values in templates. That means, in the absence of typename keyword, compiler would interpret this as multiplication of T::X with x, T::X *x; //multiply T::X with x So in such situations where ambiguity can arise, the keyword typename becomes necessity so as to

why … (three points) in catch block is exist?

試著忘記壹切 提交于 2020-01-06 23:47:05
问题 In the try catch statement we can do: try{} catch(...){} As far as I know, ... means any exception. My question is : Why the C++ standard chose this way (...) instead of just () ? while, for example, in functions if you do not need parameters you just put () : void foo(); Is it related to variadic templates in any way? 回答1: catch() would imply strongly that nothing was passed to that particular catch block. But that is not true, catch(...){ throw; } actually re-throws the exception caught by

C++ threads stack address range

﹥>﹥吖頭↗ 提交于 2020-01-05 12:15:37
问题 Does the C++ standard provide a guarantee about the non-overlapping nature of thread stacks (as in started by an std::thread )? In particular is there a guarantee that threads will have have their own, exclusive, allocated range in the process's address space for the thread stack? Where is this described in the standard? For example std::uintptr_t foo() { auto integer = int{0}; return std::bit_cast<std::uintptr_t>(&integer); ... } void bar(std::uint64_t id, std::atomic<std::uint64_t>& atomic)

May a compiler elide the evaluation of the “not-taken” branch in a constexpr function?

谁说我不能喝 提交于 2020-01-05 10:19:23
问题 While attempting to answer a question by Mehrdad, I concocted the little function below (in action at liveworkspace): template <typename T, unsigned low, unsigned high> static constexpr auto highest_index_in() -> typename std::enable_if<high >= low, unsigned>::type { return low == high ? low : high == low + 1 ? (exists<T, high>() ? high : low) : exists<T, (high + low)/2>() ? highest_index_in<T, (high+low)/2, high>() : highest_index_in<T, low, (high+low)/2>(); } // highest_index_in (where

Reusing data member storage via placement new

五迷三道 提交于 2020-01-05 07:16:21
问题 Is it allowed to reuse storage of a non-static data member and if so under what conditions? Consider the program #include<new> #include<type_traits> using T = /*some type*/; using U = /*some type*/; static_assert(std::is_object_v<T>); static_assert(std::is_object_v<U>); static_assert(sizeof(U) <= sizeof(T)); static_assert(alignof(U) <= alignof(T)); struct A { T t /*initializer*/; U* u; A() { t.~T(); u = ::new(static_cast<void*>(&t)) U /*initializer*/; } ~A() { u->~U(); ::new(static_cast<void*

Why can't one use scope resolution with member pointer dereference?

馋奶兔 提交于 2020-01-05 06:52:22
问题 Consider a simple example: struct FooParent { virtual void bar() { } }; struct Foo: FooParent { void bar() { } }; int main() { Foo foo; void (Foo::*foo_member)() = &FooParent::bar; //(foo.*FooParent::foo_member)(); foo.FooParent::bar(); } As you can see one can use scope resolution on the foo object when calling bar member function while there is no way to explicitly declare the scope for member function pointer. I accept that the syntax should be prohibited when using ->* as the operator can

Context of using declaration and ambiguous declaration

混江龙づ霸主 提交于 2020-01-05 04:25:09
问题 There is a quote from the sec. 3.4.3.2/3: Given X::m (where X is a user-declared namespace), or given ::m (where X is the global namespace), if S(X, m) is the empty set, the program is ill-formed. Otherwise, if S(X, m) has exactly one member, or if the context of the reference is a using-declaration (7.3.3), S(X, m) is the required set of declarations of m. Definition of S(X,m) is the following sec. 3.4.3.2/2: For a namespace X and name m, the namespace-qualified lookup set S(X, m) is defined

Value initialization of class object without default constructor

社会主义新天地 提交于 2020-01-05 04:21:10
问题 I am trying to understand the exact behavior of value initialization by T() or T{} for a class type T in C++11. What confuses me are these two snippets taken from http://en.cppreference.com: Value Initialization: The effects of value initialization are: [...] 1) if T is a class type with no default constructor or with a user-provided or deleted default constructor , the object is default-initialized; (since C++11) [...] so I looked up Default-Initialization: The effects of default

Can I implicitly create a trivially copiable type

Deadly 提交于 2020-01-04 14:13:41
问题 My question is this: Suppose type T is trivially copyable....can "create" an instance of this type without calling a constructor....like so: #include <type_traits> #include <cstring> using T = int; // T can be any trivially copyable type T create(const T& other) { std::aligned_storage_t<sizeof(T),alignof(T)> my_T; std::memcpy(&my_T,&other,sizeof(T)); return *reinterpret_cast<T*>(&my_T); } Is this defined behavior, or can I only copy into an existing object of type T? 回答1: The rule, from