language-lawyer

std::type_info::hash_code() uniqueness and the meaning of “should”

余生长醉 提交于 2020-01-02 01:18:27
问题 Is it meant to be guaranteed that same std::type_info::hash_code() values imply same types? Cplusplus.com seems to claim so: This function returns the same value for any two type_info objects that compare equal, and different values for distinct types that do not. [Emphasis mine] Cppreference seems to claim otherwise: Returns an unspecified value, which is identical for objects, referring to the same type. No other guarantees are given , in particular, the value can change between invocations

Is assert usable in constant expressions?

六眼飞鱼酱① 提交于 2020-01-02 00:57:10
问题 The assert -macro from <cassert> provides a concise way of ensuring that a condition is met. If the argument evaluates to true , it shall not have any further effects. However, can its invocation also be used inside a constant expression in that case? 回答1: This was dealt with by LWG 2234, which was brought back to attention after relaxed constraints on constexpr functions had been introduced. Proposed resolution : This wording is relative to N3936. Introduce the following new definition to

Why must 'auto' declarations all be of the same type?

一个人想着一个人 提交于 2020-01-02 00:49:07
问题 It appears that it is not allowed to declare multiple variables of distinct types using the auto keyword. I can't figure out the wording in the standard that would prevent it however. auto i = 1, j = 1.0; //deduction failure (several compilers) Historically I understand since you have only one decl-specifier-spec . However, the rules in the standard don't seem to preclude, in fact they encourage, that auto can be a distinct type for each. Consider these paragraphs: 8-3 Each init-declarator in

C++1y/C++14: Assignment to object outside its lifetime is not allowed in a constant expression?

你离开我真会死。 提交于 2020-01-02 00:34:30
问题 Is the following C++14/C++1y program ill-formed according to the current draft? #include <cstddef> template<typename T, size_t n> struct literal_array { T data[n]; }; template<typename T, size_t n, size_t m> constexpr literal_array<T, n+m> operator+(literal_array<T, n> a, literal_array<T, m> b) { literal_array<T, n+m> x; for (size_t i = 0; i < n; i++) x.data[i] = a.data[i]; for (size_t i = 0; i < m; i++) x.data[n+i] = b.data[i]; return x; } int main() { constexpr literal_array<int, 3> a = { 1

Does C99/C11 restrict type qualifier imply anything for functions without definition?

本小妞迷上赌 提交于 2020-01-01 13:15:58
问题 Suppose we have a function declaration for which we do not have access to its definition: void f(int * restrict p, int * restrict q, int * restrict r); Since we do not know how the pointers will be accessed, we cannot know if a call will trigger undefined behavior or not -- even if we are passing the same pointer, like the example at 6.7.3.1.10 explains: The function parameter declarations: void h(int n, int * restrict p, int * restrict q, int * restrict r) { int i; for (i = 0; i < n; i++) p

Is (++i)++ undefined behavior?

寵の児 提交于 2020-01-01 09:42:36
问题 Is (++i)++ undefined behavior? Is it possible that the side effect of prefix increment happens after retrieving the incremented object for postfix increment to operate on? That would seem strange to me. My gut feeling says this is undefined in C++03 and well-defined in C++11. Am I right? 回答1: My gut feeling says this is undefined in C++03 and well-defined in C++0x. Yes you are right. The behaviour is undefined in C++03 because you are trying to modify i more than once between two sequence

Requirements on standard library allocator pointer types

99封情书 提交于 2020-01-01 09:02:34
问题 I am trying to write a quadtree sparse matrix class. In short, a quadtree_matrix<T> is either the zero matrix or a quadruple (ne, nw, se, sw) of quadtree_matrix<T> . I'd like eventually to test different allocation schemes since this will probably impact the performance of linear algebra operations. So I will also template quadtree_matrix on a standard allocator type, so that I can reuse existing allocators. I will have to allocate two different kind of data: either a T , or a node , which

Does casting a char array to another type violate strict-aliasing rules?

╄→尐↘猪︶ㄣ 提交于 2020-01-01 08:27:08
问题 Consider these two functions: int f1() { alignas(int) char buf[sizeof(int)] = {}; return *reinterpret_cast<int*>(buf); } int f2() { alignas(int) char buf[sizeof(int)] = {}; char* ptr = buf; return *reinterpret_cast<int*>(ptr); } GCC warns that the first violates strict-aliasing rules. But the second is OK. Clang accepts both without complaint. Is the warning legitimate? 回答1: The warning is legitimate. f2 is not OK (it is undefined behaviour), it just doesn't provoke the warning. I suspect the

Is it permissible for standard library implementation to have class definition that is different from the C++ standard?

我是研究僧i 提交于 2020-01-01 08:21:53
问题 The following code successfully compiled with clang and MSVC but fail to compile in GCC 6.1.0. #include <memory> template<typename R, typename T, typename... Args> T* test(R(T::*)(Args...) const) { return nullptr; } int main() { using T = std::shared_ptr<int>; T* p = test(&T::get); } with the following error message prog.cc: In function 'int main()': prog.cc:13:16: error: invalid conversion from 'std::__shared_ptr<int, (__gnu_cxx::_Lock_policy)2u>*' to 'T* {aka std::shared_ptr<int>*}' [

Is it permissible for standard library implementation to have class definition that is different from the C++ standard?

若如初见. 提交于 2020-01-01 08:21:45
问题 The following code successfully compiled with clang and MSVC but fail to compile in GCC 6.1.0. #include <memory> template<typename R, typename T, typename... Args> T* test(R(T::*)(Args...) const) { return nullptr; } int main() { using T = std::shared_ptr<int>; T* p = test(&T::get); } with the following error message prog.cc: In function 'int main()': prog.cc:13:16: error: invalid conversion from 'std::__shared_ptr<int, (__gnu_cxx::_Lock_policy)2u>*' to 'T* {aka std::shared_ptr<int>*}' [