language-lawyer

Does this C++ static analysis rule make sense as is?

主宰稳场 提交于 2020-01-01 04:24:26
问题 I'm implementing some C++ static analysis rules, and one of them prohibits a function from returning a reference or pointer to a reference parameter of the function, i.e. the following are all non-compliant: int *f(int& x) { return &x; } // #1 const int *g(const int& x) { return &x; } // #2 int& h(int& x) { return x; } // #3 const int& m(const int& x) { return x; } // #4 The justification given for this is that "It is implementation-defined behaviour whether the reference parameter is a

What does the standard say about char arrays as template arguments?

别来无恙 提交于 2020-01-01 04:22:27
问题 During my research for an answer for this question I found (I did not know that before) that gcc and clang allow char arrays to be template arguments if they are declared static . E.g. this code compiles with gcc and clang: #include <type_traits> template <int N, const char (&string)[N]> auto foo() { if constexpr (string[0] == 'i') return 0; else return 3.14f; } void bar() { static constexpr char string1[] = "int"; static constexpr char string2[] = "float"; auto i = foo<sizeof(string1),

Clang and GCC disagree in auto specifier for non-type template parameter in a casting C++17

£可爱£侵袭症+ 提交于 2020-01-01 04:04:05
问题 I basically have a class that depends on a non-type template parameter. I defined a casting so an object of non-type template parameter N can convert to another of M . I have a minimal example that can reproduce the situation: template<auto Integral> class Test{ public: typedef decltype(Integral) value_type; static constexpr value_type N = Integral; constexpr Test (const value_type& x = 0); template<auto Integral2> constexpr explicit operator Test<Integral2>() const; private: value_type n; };

Clang and GCC disagree in auto specifier for non-type template parameter in a casting C++17

吃可爱长大的小学妹 提交于 2020-01-01 04:04:01
问题 I basically have a class that depends on a non-type template parameter. I defined a casting so an object of non-type template parameter N can convert to another of M . I have a minimal example that can reproduce the situation: template<auto Integral> class Test{ public: typedef decltype(Integral) value_type; static constexpr value_type N = Integral; constexpr Test (const value_type& x = 0); template<auto Integral2> constexpr explicit operator Test<Integral2>() const; private: value_type n; };

Arrow (->) operator precedence/priority is lowest, or priority of assignment/combined assignment is lowest?

蹲街弑〆低调 提交于 2020-01-01 04:01:31
问题 JLS: The lowest precedence operator is the arrow of a lambda expression (->) , followed by the assignment operators. Followed in which direction (increasing priority, decreasing priority)? - "followed" means assignment has higher priority or lower priority (with respect to arrow operator)? I guess, in increasing, because "lowest" (for arrow) means absolutely lowest. As I understand, arrow (->) should be at the very bottom of this Princeton operator precedence table (that is below all

Explicit default constructor

拈花ヽ惹草 提交于 2020-01-01 03:59:06
问题 This code compiles fine with GCC 5.X, MSVC, but GCC 6.X gives error: "converting to 'a' from initializer list would use explicit constructor 'a::a()'" , clang "chosen constructor is explicit in copy-initialization" . Removing explicit or changing to a c{} fixes the problem, but I`m curious why it works this way. class a { public: explicit a () {} }; struct b { a c; }; int main() { b d{}; } 回答1: b is an aggregate. When you initialize it using an initializer list, the elements in the list will

Managing trivial types

て烟熏妆下的殇ゞ 提交于 2020-01-01 02:09:32
问题 I have found the intricacies of trivial types in C++ non-trivial to understand and hope someone can enlighten me on the following. Given type T , storage for T allocated using ::operator new(std::size_t) or ::operator new[](std::size_t) or std::aligned_storage , and a void * p pointing to a location in that storage suitably aligned for T so that it may be constructed at p : If std::is_trivially_default_constructible<T>::value holds, is the code invoking undefined behavior when code skips

Does a reference declaration introduce a new name for the referent?

淺唱寂寞╮ 提交于 2020-01-01 01:59:08
问题 In this question we've learnt that RVO cannot be applied to an expression like p.first . In comments it was also suggested that RVO is generally not applied to an expression like r after a declaration like auto& r = p.first . It is less clear whether the standard mandates this behaviour. in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function parameter or a variable introduced by the exception

Does copying an empty object involve accessing it

牧云@^-^@ 提交于 2020-01-01 01:53:13
问题 Inspired from this question. struct E {}; E e; E f(e); // Accesses e? To access is to read or modify the value of an object The empty class has an implicitly defined copy constructor The implicitly-defined copy/move constructor for a non-union class X performs a memberwise copy/move of its bases and members. [...] The order of initialization is the same as the order of initialization of bases and members in a user-defined constructor. Let x be either the parameter of the constructor or, for

What is a composite type in C?

限于喜欢 提交于 2019-12-31 21:42:12
问题 From §6.2.7.5 (page 66): EXAMPLE Given the following two file scope declarations: int f(int (*)(), double (*)[3]); int f(int (*)(char *), double (*)[]); The resulting composite type for the function is: int f(int (*)(char *), double (*)[3]); Above the example, they explain that a composite type is a type, compatible with two different types. I would intuitively understand the phrase "composite type" as meaning "structures and unions", which appears to be way off-target. What is a composite