language-lawyer

Why is a constexpr function on a reference not constexpr?

◇◆丶佛笑我妖孽 提交于 2020-01-21 10:59:04
问题 Consider the following function: template <size_t S1, size_t S2> auto concatenate(std::array<uint8_t, S1> &data1, std::array<uint8_t, S2> &data2) { std::array<uint8_t, data1.size() + data2.size()> result; auto iter = std::copy(data1.begin(), data1.end(), result.begin()); std::copy(data2.begin(), data2.end(), iter); return result; } int main() { std::array<uint8_t, 1> data1{ 0x00 }; std::array<uint8_t, 1> data2{ 0xFF }; auto result = concatenate(data1, data2); return 0; } When compiled using

What is a temporary object with static storage duration

霸气de小男生 提交于 2020-01-21 10:38:57
问题 Inspired from this answer, from [expr.const] A constant expression is either a glvalue core constant expression that refers to an entity that is a permitted result of a constant expression (as defined below), or a prvalue core constant expression whose value satisfies the following constraints: if the value is an object of class type, each non-static data member of reference type refers to an entity that is a permitted result of a constant expression, if the value is of pointer type, it

Declaring a member function with a typedef coming from a metafunction

ぐ巨炮叔叔 提交于 2020-01-21 04:45:18
问题 Consider the following code: template <class> struct Foo_s { using type = void(); }; template <class> using Foo_u = void(); template <class T> struct Bar { Foo_u<void> foo1; // OK typename Foo_s<void>::type foo2; // OK Foo_u<T> foo3; // OK typename Foo_s<T>::type foo4; // Boom. }; template struct Bar<void>; The declaration of foo4 fails on GCC 7.2, Clang 5.0.0 and MSVC 19.10.25017. GCC: <source>: In instantiation of 'struct Bar<void>': 18 : <source>:18:17: required from here 15 : <source>:15

Is (or will be) the use of familiar template syntax in lambda expressions allowed?

可紊 提交于 2020-01-20 04:32:47
问题 C++14 introduced generic lambdas. While skimming through the related proposals I found N3418 by Faisal Vali, Herb Sutter and Dave Abrahams . Therein section 2.2 is titled : 2.2 Allow the use of familiar template syntax in lambda expressions and the following code examples include snippets like this one []<int N>(int (&a)[N]) {} Since such things fail to compile (with gcc, clang and Visual Studio), some questions come up : Is this an implementation issue ? What stopped this part from being

C - is an indeterminate value indeterminable?

白昼怎懂夜的黑 提交于 2020-01-19 03:51:09
问题 According to this post an indeterminate value is: 3.17.2 1 indeterminate value either an unspecified value or a trap representation According to google, the definition of indeterminate is: Not certain, known, or established Left doubtful; vague. According to thefreedictionary, determinable is: capable of being determined According to merriam-webster, to determine (in the particular context) is: to find out or come to a decision about by investigation, reasoning, or calculation So, common

Reading uninitialized variable [duplicate]

╄→гoц情女王★ 提交于 2020-01-16 18:05:47
问题 This question already has answers here : What happens when I print an uninitialized variable in C++? (4 answers) Closed 3 years ago . Reading uninitialized variables leads to undefined behavior, e.g. #include <iostream> int main() { int a; std::cout << a << std::endl; // undefined behavior } Can someone give a formal explanation of this fact? 回答1: Here is the relevant section, I think: 4.1 Lvalue-to-rvalue conversion 1 - A glvalue of a non-function, non-array type T can be converted to a

Is the main() function odr-used?

只谈情不闲聊 提交于 2020-01-15 11:55:06
问题 Is the main() function odr-used? E.g in the simple program like this: int main() { } 回答1: No, it is not. Not in your simple program. [basic.def.odr] 3 A function whose name appears as a potentially-evaluated expression is odr-used if it is the unique lookup result or the selected member of a set of overloaded functions ([basic.lookup], [over.match], [over.over]), unless it is a pure virtual function and either its name is not explicitly qualified or the expression forms a pointer to member (

sqrt() - Why am I allowed to provide an argument of int and not only double and the output is also right?

久未见 提交于 2020-01-14 22:02:14
问题 I wondering why the compiler let this pass and is giving the right output, although sqrt() from its prototype normally should only get an double value as argument: In C99 the declaration of the prototype is: double sqrt (double x); #include <stdio.h> #include <math.h> int main (void) { int i = 9; printf("\t Number \t\t Square Root of Number\n\n"); printf("\t %d \t\t\t %f \n",i, sqrt(i)); } Output: Number Square Root of Number 9 3.000000 Why does the compiler not throw a warning at least and

sqrt() - Why am I allowed to provide an argument of int and not only double and the output is also right?

谁说胖子不能爱 提交于 2020-01-14 21:48:14
问题 I wondering why the compiler let this pass and is giving the right output, although sqrt() from its prototype normally should only get an double value as argument: In C99 the declaration of the prototype is: double sqrt (double x); #include <stdio.h> #include <math.h> int main (void) { int i = 9; printf("\t Number \t\t Square Root of Number\n\n"); printf("\t %d \t\t\t %f \n",i, sqrt(i)); } Output: Number Square Root of Number 9 3.000000 Why does the compiler not throw a warning at least and

Is it undefined behavior to do runtime borrow management with the help of raw pointers in Rust?

…衆ロ難τιáo~ 提交于 2020-01-14 19:50:51
问题 As part of binding a C API to Rust, I have a mutable reference ph: &mut Ph , a struct struct EnsureValidContext<'a> { ph: &'a mut Ph } , and some methods: impl Ph { pub fn print(&mut self, s: &str) { /*...*/ } pub fn with_context<F, R>(&mut self, ctx: &Context, f: F) -> Result<R, InvalidContextError> where F: Fn(EnsureValidContext) -> R, { /*...*/ } /* some others */ } impl<'a> EnsureValidContext<'a> { pub fn print(&mut self, s: &str) { self.ph.print(s) } pub fn close(self) {} /* some others