language-lawyer

Is UB in unevaluated context (e.g. requires-expressions) still UB?

半世苍凉 提交于 2021-02-07 05:23:24
问题 The C++ 20 draft [concept.default.init] does not precisely define default_initializable template<class T> concept default_initializable = constructible_from<T> && requires { T{}; } && is-default-initializable <T>; // exposition-only and describe what is-default-initializable should do with the following words: For a type T , is-default-initializable <T> is true if and only if the variable definition T t; is well-formed for some invented variable t; otherwise it is false. Access checking is

What is a full expression in C?

 ̄綄美尐妖づ 提交于 2021-02-07 05:21:52
问题 I study C language from "C Primer Plus" book by Stephen Prata and it came to the point : "A full expression is one that’s not a subexpression of a larger expression.Examples of full expressions include the expression in an expression statement and the expression serving as a test condition for a while loop" I can't understand clearly what is the exact definition of full expressions and why the book considers test conditions are full expressions. Could any one explain clearly what is meant by

Is this a universal reference? Does std::forward make sense here?

让人想犯罪 __ 提交于 2021-02-07 05:20:20
问题 Consider this snippet of code, which uses the common idiom of having a function template construct an instance of a class template specialized on a deduced type, as seen with std::make_unique and std::make_tuple , for example: template <typename T> struct foo { std::decay_t<T> v_; foo(T&& v) : v_(std::forward<T>(v)) {} }; template <typename U> foo<U> make_foo(U&& v) { return { std::forward<U>(v) }; } In the context of Scott Meyers' "universal references", the argument to make_foo is a

Is this a universal reference? Does std::forward make sense here?

蓝咒 提交于 2021-02-07 05:20:14
问题 Consider this snippet of code, which uses the common idiom of having a function template construct an instance of a class template specialized on a deduced type, as seen with std::make_unique and std::make_tuple , for example: template <typename T> struct foo { std::decay_t<T> v_; foo(T&& v) : v_(std::forward<T>(v)) {} }; template <typename U> foo<U> make_foo(U&& v) { return { std::forward<U>(v) }; } In the context of Scott Meyers' "universal references", the argument to make_foo is a

Is this a universal reference? Does std::forward make sense here?

二次信任 提交于 2021-02-07 05:18:43
问题 Consider this snippet of code, which uses the common idiom of having a function template construct an instance of a class template specialized on a deduced type, as seen with std::make_unique and std::make_tuple , for example: template <typename T> struct foo { std::decay_t<T> v_; foo(T&& v) : v_(std::forward<T>(v)) {} }; template <typename U> foo<U> make_foo(U&& v) { return { std::forward<U>(v) }; } In the context of Scott Meyers' "universal references", the argument to make_foo is a

Is it technically impossible to implement memcpy from scratch in Standard C?

一世执手 提交于 2021-02-07 05:11:59
问题 Howard Chu writes: In the latest C spec it is impossible to write a "legal" implementation of malloc or memcpy. Is this right? My impression is that in the past, the intent (at least) of the standard was that something like this would work: void * memcpy(void * restrict destination, const void * restrict source, size_t nbytes) { size_t i; unsigned char *dst = (unsigned char *) destination; const unsigned char *src = (const unsigned char *) source; for (i = 0; i < nbytes; i++) dst[i] = src[i];

Is it technically impossible to implement memcpy from scratch in Standard C?

霸气de小男生 提交于 2021-02-07 05:11:26
问题 Howard Chu writes: In the latest C spec it is impossible to write a "legal" implementation of malloc or memcpy. Is this right? My impression is that in the past, the intent (at least) of the standard was that something like this would work: void * memcpy(void * restrict destination, const void * restrict source, size_t nbytes) { size_t i; unsigned char *dst = (unsigned char *) destination; const unsigned char *src = (const unsigned char *) source; for (i = 0; i < nbytes; i++) dst[i] = src[i];

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

醉酒当歌 提交于 2021-02-07 05:10:51
问题 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

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

▼魔方 西西 提交于 2021-02-07 05:08:53
问题 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

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

天大地大妈咪最大 提交于 2021-02-07 05:05:11
问题 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