language-lawyer

Since a string literal is considered an lvalue, why must the binding lvalue reference be const?

对着背影说爱祢 提交于 2021-02-07 05:05:02
问题 I know there are topics that are similar to this one already (such as this). The example given in this topic was this: std::string & rs1 = std::string(); Clearly, that std::string() is an rvalue. However, my question is why is s1 legal while s2 is not? const std::string& s1 = "String literal"; std::string& s2 = "String literal"; The standard clearly states that string literals are lvalues (which is understandable since they are technically const char* behind the scenes). When I compile s2

Behavior of & followed by * operator

喜你入骨 提交于 2021-02-07 05:01:59
问题 I have a question about the behavior of the address-of operator followed by a dereference operator. Let's take a look at the expression &*p where p is of type int * . The C11 standard (section 6.5.3.2) says: The unary & operator yields the address of its operand. If the operand has type ‘‘ type ’’, the result has type ‘‘pointer to type ’’. If the operand is the result of a unary * operator, neither that operator nor the & operator is evaluated and the result is as if both were omitted, except

Is the usage of reinterpret_cast on a memcpy buffer UB?

半世苍凉 提交于 2021-02-07 02:44:09
问题 Given the code struct A {}; auto obj = new A; std::vector<unsigned char> buffer; buffer.resize(sizeof(obj)); std::memcpy(buffer.data(), &obj, sizeof(obj)); // this copies the pointer, not the object! // ... auto ptr = *reinterpret_cast<A**>(buffer.data()); // is this UB? delete ptr; is the usage of reinterpret_cast in this case UB? I would say yes, because memcpy doesn't start the lifetime of an instance hence violating the strict aliasing rule (which is why std::bit_cast has been added to C+

Function template overload resolution, dependent and non-dependent parameters

爷,独闯天下 提交于 2021-02-07 02:21:39
问题 Given the following program #include <iostream> template<class T> struct id { using type = T; }; template<class T1, class T2> int func(T1, T2) { return 0; } template<class T1, class T2> int func(typename id<T1>::type, typename id<T2>::type) { return 1; } int main() { std::cout << func<int, int>(0, 0) << std::endl; } GCC and Clang both prints 1 for this program. Is this program guaranteed to print 1 by the standard? I tried finding the answer here but couldn't decipher it. It looks like the

Function template overload resolution, dependent and non-dependent parameters

一曲冷凌霜 提交于 2021-02-07 02:07:22
问题 Given the following program #include <iostream> template<class T> struct id { using type = T; }; template<class T1, class T2> int func(T1, T2) { return 0; } template<class T1, class T2> int func(typename id<T1>::type, typename id<T2>::type) { return 1; } int main() { std::cout << func<int, int>(0, 0) << std::endl; } GCC and Clang both prints 1 for this program. Is this program guaranteed to print 1 by the standard? I tried finding the answer here but couldn't decipher it. It looks like the

Constrained member functions and explicit template instantiation

泪湿孤枕 提交于 2021-02-07 01:21:45
问题 G++ and Clang++ agree that the following snippet is not valid C++: template<int dim, int rank> struct Tensor {}; template<int dim> double InnerProduct(Tensor<dim, 1> const &, Tensor<dim, 1> const &) { return 0.0; } template<int dim> double DoubleInnerProduct(Tensor<dim, 2> const &, Tensor<dim, 2> const &) { return 0.0; } template<int dim, int rank> class Field { private: static double Dot(Tensor<dim, rank> const &u, Tensor<dim, rank> const &v) requires (rank == 1) { return InnerProduct(u, v);

Curiously mutually recurring class definitions

梦想的初衷 提交于 2021-02-06 15:01:40
问题 I want type declarations in two classes to mutually depend on each other. Here is a first example that compiles both with clang and gcc: template <class Sum> struct A { using X = char; // (1) using Z = typename Sum::B::Y; // (2) }; template <class Sum> struct B { using Y = typename Sum::A::X; }; struct AplusB { using A = ::A<AplusB>; using B = ::B<AplusB>; }; AplusB::A::Z z; int main() {} There is an interesting moment, however. If you swap lines (1) and (2), then it will fail to compile with

Is a c++11 variadic function template overload with a dependent type ambiguous?

匆匆过客 提交于 2021-02-06 11:06:58
问题 The following code is a textbook example of a recursive variadic function overload. In both clang and GCC, it compiles cleanly, and main returns 36 (as expected): template <typename T> int add(T val) { return val; } template <typename FirstTypeT, typename... RestT> int add(FirstTypeT first_value, RestT... rest) { return first_value + add<RestT...>(rest...); } int main(void) { return add(12, 12, 12); } However, here is a slight modification. It uses a dependent type in the template definition

Is a c++11 variadic function template overload with a dependent type ambiguous?

倖福魔咒の 提交于 2021-02-06 11:02:01
问题 The following code is a textbook example of a recursive variadic function overload. In both clang and GCC, it compiles cleanly, and main returns 36 (as expected): template <typename T> int add(T val) { return val; } template <typename FirstTypeT, typename... RestT> int add(FirstTypeT first_value, RestT... rest) { return first_value + add<RestT...>(rest...); } int main(void) { return add(12, 12, 12); } However, here is a slight modification. It uses a dependent type in the template definition

Is it well-defined to use a pointer pointing to one-past-malloc?

我是研究僧i 提交于 2021-02-05 20:14:34
问题 In C, it is perfectly well to make a pointer that points to one past the last element of an array and use it in pointer arithmetics, as long as you don't dereference it: int a[5], *p = a+5, diff = p-a; // Well-defined However, these are UBs: p = a+6; int b = *(a+5), diff = p-a; // Dereferencing and pointer arithmetic Now I have a question: Does this apply to dynamically allocated memory? Assume I'm only using a pointer pointing to one-past-the-last in pointer arithmetics, without