language-lawyer

Is there any drawbacks in providing operator+ or operator- to bidirectional iterators?

你说的曾经没有我的故事 提交于 2020-01-13 12:08:15
问题 The bidirectional iterators have no luxuries like random access iterators, and hence need to depend upon the std::next and std::prev, when someone needs to do operations like std::set<int> s{ 1, 2, 3, 4, 5 }; //std::set<int> s2(s.cbegin(), s.cbegin() + 2); // won't work as there is no operator+ for std::set::const_iterator std::set<int> s2(s.cbegin(), std::next(s.cbegin(), 2)); //std::set<int> s3(s.cbegin(), s.cend() - 2); // won't work as there is no operator- for std::set::const_iterator

Is it guaranteed that sizeof(T[N]) == N * sizeof(T)?

本秂侑毒 提交于 2020-01-13 08:57:07
问题 I had always assumed that the size of an array of N elements of type T , as returned by sizeof was guaranteed to be exactly N times sizeof(T) . The comments on this question made me doubt it though. There are claims from reputable users that arrays may contain padding, which would break the equality. Of course such platforms may not exist, but are they allowed? If allowed, this would break many common idioms, such as calculating the needed storage for an array with N * sizeof(T) , or

Type requirements for std::map

Deadly 提交于 2020-01-13 07:58:22
问题 Today I created a map, where the value type has no default constructor. I was surprised that I could not use operator[] to insert the elements to this map, but I had to use the insert method. So, what exactly are requirements for the key and value types for std::map? Here is short example : #include <map> struct A { A(int){} }; int main() { std::map< int, A > m; A a1(2); A a2(3); A a3(4); m[5] = a1; m[3] = a2; m[2] = a3; } I am compiling like this : [vladimir@sandbox tmp]$ g++ b5.cpp -Wall

What's wrong with std::valarray's operator*?

寵の児 提交于 2020-01-13 07:53:07
问题 Consider the following MCVE, where I have two value arrays where w is two times v (try it out here): #include <valarray> using namespace std; int main() { valarray<int> v { 1, 2, 3 }; for ([[maybe_unused]] auto x : v) {} // Ok auto w = v * 2; // Leads to failure in loop below //valarray<int> w = v * 2; // Works //auto w = v*=2; // Works //auto w = v; w *= 2; // Works for ([[maybe_unused]] auto x : w) {} // Failure here } This example fails to compile with clang and gcc at the last loop with

Difference between forward declaration in argument vs “normal” forward declaration

二次信任 提交于 2020-01-13 04:48:10
问题 What is - if any - the difference between a forward declaration in a (template) argument (using an elaborated type specifier) and a "normal" forward declaration? void foo(struct bar *); // vs struct bar; void foo(bar *); // ------ and also ------- std::unique_ptr<class Baz> one; // vs class Baz; std::unique_ptr<Baz> two; 回答1: Let's begin by noting that "forward declaration" is a colloquialism used to refer to a certain common practical use of certain kinds of declarations. There is no such

How to check whether an int variable contains a legal (not trap representation) value?

痞子三分冷 提交于 2020-01-12 23:32:08
问题 Context: This is mainly a followup to that other question. OP wanted to guess whether a variable contained an int or not, and my first thought was that in C (as in C++) an int variable could only contain an int value. And Eric Postpischil reminded me that trap representations were allowed per standard for the int type... Of course, I know that most modern system only use 2-complement representations of integers and no padding bits, meaning that no trap representation can be observed.

How to check whether an int variable contains a legal (not trap representation) value?

这一生的挚爱 提交于 2020-01-12 23:31:24
问题 Context: This is mainly a followup to that other question. OP wanted to guess whether a variable contained an int or not, and my first thought was that in C (as in C++) an int variable could only contain an int value. And Eric Postpischil reminded me that trap representations were allowed per standard for the int type... Of course, I know that most modern system only use 2-complement representations of integers and no padding bits, meaning that no trap representation can be observed.

C++ Order of Declaration (in Multi-variable Declaration Line)

拟墨画扇 提交于 2020-01-12 15:18:10
问题 I use the following in my C++ code: int a = 0, b = a; I would like to know if this behaviour is reliable and well defined (left to right order of name declaration) and that my code will not break with other compilers with an undeclared name error. If not reliable, I would break the statement: int a = 0; int b = a; Thank you. 回答1: I believe the answer is no. It is subject to core active issue 1342 which says: It is not clear what, if anything, in the existing specification requires that the

C++ Order of Declaration (in Multi-variable Declaration Line)

随声附和 提交于 2020-01-12 15:17:09
问题 I use the following in my C++ code: int a = 0, b = a; I would like to know if this behaviour is reliable and well defined (left to right order of name declaration) and that my code will not break with other compilers with an undeclared name error. If not reliable, I would break the statement: int a = 0; int b = a; Thank you. 回答1: I believe the answer is no. It is subject to core active issue 1342 which says: It is not clear what, if anything, in the existing specification requires that the

Mysterious line in stack trace

喜夏-厌秋 提交于 2020-01-12 14:31:11
问题 While investigating a stack trace discrepancy when composing another answer, I came across a behavior I do not understand. Consider the following test program (this is as far down as I could narrow it): interface TestInterface <U> { void test (U u); } static class Test <T extends Test<T>> implements TestInterface<T> { // line 11 @Override public void test (T t) { throw new RuntimeException("My exception"); // line 13 } } static class TestA extends Test<TestA> { } static class TestB extends