language-lawyer

Does `const &&` bind to all prvalues (and xvalues)?

坚强是说给别人听的谎言 提交于 2019-12-19 17:34:10
问题 The C++ standard defines the following functions deleted; template <class T> void ref(const T&&) = delete; template <class T> void cref(const T&&) = delete; This is to aid in ensuring that the functions are not misused by disallowing them from binding to temporary values (rvalues). Does const && bind to all rvalues, specifically prvalues? Would const && bind to all "moved objects" (xvalues; basically something returned from std::move or similar)? I can reason that it should, but I don't have

Does `const &&` bind to all prvalues (and xvalues)?

做~自己de王妃 提交于 2019-12-19 17:32:21
问题 The C++ standard defines the following functions deleted; template <class T> void ref(const T&&) = delete; template <class T> void cref(const T&&) = delete; This is to aid in ensuring that the functions are not misused by disallowing them from binding to temporary values (rvalues). Does const && bind to all rvalues, specifically prvalues? Would const && bind to all "moved objects" (xvalues; basically something returned from std::move or similar)? I can reason that it should, but I don't have

SFINAE method completely disables base class's template method in clang

梦想的初衷 提交于 2019-12-19 17:15:33
问题 #include <iostream> #include <utility> struct B { template<typename T, std::enable_if_t<std::is_same<T, int>::value>* = nullptr> void foo(T) { std::cout<<"B::foo"<<std::endl; } }; struct D: B { using B::foo; template<typename T, std::enable_if_t<std::is_same<T, std::string>::value>* = nullptr> void foo(T) { std::cout<<"D::foo"<<std::endl; } }; int main() { D d; d.foo(2); // gcc: print "B::foo"; clang: compile error return 0; } Let's say we want to expose both foo() overloads in derived class

SFINAE method completely disables base class's template method in clang

佐手、 提交于 2019-12-19 17:15:33
问题 #include <iostream> #include <utility> struct B { template<typename T, std::enable_if_t<std::is_same<T, int>::value>* = nullptr> void foo(T) { std::cout<<"B::foo"<<std::endl; } }; struct D: B { using B::foo; template<typename T, std::enable_if_t<std::is_same<T, std::string>::value>* = nullptr> void foo(T) { std::cout<<"D::foo"<<std::endl; } }; int main() { D d; d.foo(2); // gcc: print "B::foo"; clang: compile error return 0; } Let's say we want to expose both foo() overloads in derived class

Ambiguous name lookup with using-directive

眉间皱痕 提交于 2019-12-19 16:32:12
问题 It's not allowed to put a namespace and a class with the same name into one declarative region, i.e. namespace A {} class A{}; is ill-formed (see §3.3.1/4). However, one can introduce the name of either one via a using-directive: namespace N { namespace A {int i;} } struct A {static int i;}; using namespace N; int i = A::i; // The global struct, or namespace N::A? Is this code ill-formed? VC++ thinks so, as well as Clang: main.cpp:7:9: error: reference to 'A' is ambiguous int i = A::i; ^ main

Initializing scalars with braces

我们两清 提交于 2019-12-19 12:53:14
问题 In C and C++, one can initialize arrays and structs using braces: int a[] = {2, 3, 5, 7}; entry e = {"answer", 42}; However, in a talk from 2007, Bjarne mentions that this syntax also works for scalars. I tried it: int i = {7}; And it actually works! What is the rationale behind allowing the initialization of scalars with braces? Note: I am specifically not talking about C++11 uniform initialization. This is good old C89 and C++98. 回答1: What is the rationale behind allowing the initialization

The apparent underspecification of one-past-the-end subscripting: for both raw arrays and std::vector. Has it been resolved decisively already?

☆樱花仙子☆ 提交于 2019-12-19 09:36:56
问题 It has been asked before in various forms, but since the language specification appears to be quite dynamic in this regard (or at least was dynamic when some SO discussions of this matter took place), it might make sense to revisit the matter in light of any more recent developments, if any exist. So, the question is, again, whether a combination of & and subscript is a valid way to obtain a pointer to the imaginary past-the-end element of an array int a[42] = {}; &a[42]; It was considered

Are pointers to allocated memory outside object's lifetime “invalid pointer[s]” or “pointer[s] to an object”?

这一生的挚爱 提交于 2019-12-19 09:25:00
问题 C++17 (draft N4659) [basic.compound]/3 says: Every value of pointer type is one of the following: a pointer to an object or function (the pointer is said to point to the object or function), or a pointer past the end of an object ([expr.add]), or the null pointer value ([conv.ptr]) for that type, or an invalid pointer value. To which of these categories belong pointers to allocated memory outside the lifetime of objects, specifically the values of a at // (1) through // (3) and b at // (4) in

Sequence point after a return statement?

跟風遠走 提交于 2019-12-19 09:22:36
问题 In my answer to a question here I explained what happened when postfix ++ was used on a global variable on the same line as a return statement. The informative appendix C of C11 states that there is a sequence point immediately after a return and refers to normative chapter 6.8.6.4, where no text regarding sequence points can be found. Where in the C standard can I find normative text stating that there is a sequence point after a return statement? (I only found normative text stating this

Is taking the address of a member of an uninitialized object well defined?

心已入冬 提交于 2019-12-19 08:14:07
问题 Consider the following example. When bar is constructed, it gives it's base type ( foo ) constructor the address of my_member.y where my_member is data member that hasn't been initialized yet. struct foo { foo(int * p_x) : x(p_x) {} int * x; }; struct member { member(int p_y) : y(p_y) {} int y; }; struct bar : foo { bar() : foo(&my_member.y), my_member(42) {} member my_member; }; #include <iostream> int main() { bar my_bar; std::cout << *my_bar.x; } Is this well defined? Is it legal to take