Questions tagged [language-lawyer]

For questions about the intricacies of formal or authoritative specifications of programming languages.

Typical questions concern gaps between "what will usually work in practice" and "what the spec actually guarantees", but problems with understanding the structure of the spec are also on topic.

Use this tag for questions where you are interested in the formal specification for a certain behavior in the given programming language, even though your question might otherwise have no practical use, or if the code posted would not make sense in a real-world application.

Always combine this tag with a programming language tag.

7856 questions
63
votes
2 answers

Does new char actually guarantee aligned memory for a class type?

Is allocating a buffer via new char[sizeof(T)] guaranteed to allocate memory which is properly aligned for the type T, where all members of T has their natural, implementation defined, alignment (that is, you have not used the alignas keyword to…
edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267
62
votes
5 answers

Do we really need "enum class" in C++11?

When we have, struct E { enum E_ { HELLO }; }; // 'E' is inheritable then why do we need, enum class E { HELLO }; // 'E' is not inheritable IMO 2nd version doesn't offer more features than the 1st. I don't think that enum class is introduced just…
iammilind
  • 68,093
  • 33
  • 169
  • 336
62
votes
2 answers

Changed rules for protected constructors in C++17?

I have this test case: struct A{ protected: A(){} }; struct B: A{}; struct C: A{ C(){} }; struct D: A{ D() = default; }; int main(){ (void)B{}; (void)C{}; (void)D{}; } Both gcc and clang compile it in C++11 and C++14 mode. Both fail in…
Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
62
votes
2 answers

Is it spread "syntax" or the spread "operator"?

I've heard ... referred to both as 'spread syntax' and 'the spread operator', with the latter being a lot more popular. The URL of the relevant MDN documentation suggests that it was initially referred to as the spread operator but later changed to…
Andrew Li
  • 55,805
  • 14
  • 125
  • 143
62
votes
5 answers

If two objects are declared in a single line, in which order are they constructed?

Let's say a class has been defined as class A { //..... }; and now I am creating two objects as A a,b; In what order are a and b created? Is it defined by the standard?
pasha
  • 2,035
  • 20
  • 34
62
votes
3 answers

Why does `int ;` compile fine in C, but not in C++?

Consider the following program (see live demo here). #include int main(void) { int ; // Missing variable name puts("Surprise"); } My compiler, gcc 4.8.1, gives the below warning: [Warning] useless type name in empty…
Destructor
  • 14,123
  • 11
  • 61
  • 126
62
votes
6 answers

Is 0 an octal or a decimal in C?

I have read this. It's octal in C++ and decimal in Java. But no description about C? Is it going to make any difference if 0 is octal or decimal? This is the question asked by my interviewer. I said no and I explained that it is always 0 regardless…
Gibbs
  • 21,904
  • 13
  • 74
  • 138
61
votes
3 answers

What is the type of null literal?

Dear all, I wonder what is the type of null literal in C#? In Java, the null literal is of the special null type: There is also a special null type, the type of the expression null, which has no name. Because the null type has no name, it is…
Vlad
  • 35,022
  • 6
  • 77
  • 199
61
votes
2 answers

Why did the range based 'for' loop specification change in C++17?

I was looking over some ugly code (that was modifying the underlying sequence while iterating), and to explore the definition of the range-based for loop, I went to cppreference. There I noticed something strange: The range based for loop changed…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
61
votes
1 answer

Why does C++11 contain an odd clause about comparing void pointers?

While checking the references for another question, I noticed an odd clause in C++11, at [expr.rel] ¶3: Pointers to void (after pointer conversions) can be compared, with a result defined as follows: If both pointers represent the same address or…
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
61
votes
4 answers

Class static variable initialization order

I have a class A which has two static variables. I'd like to initialize one with another, unrelated static variable, just like this: #include class A { public: static int a; static int b; }; int A::a = 200; int a = 100; int A::b…
QuantumPlus
  • 473
  • 3
  • 6
61
votes
3 answers

Is x = std::move(x) undefined?

Let x be a variable of some type that has been previously initialized. Is the following line: x = std::move(x) undefined? Where is this in the standard and what does it say about it?
a06e
  • 18,594
  • 33
  • 93
  • 169
60
votes
4 answers

Is unevaluated division by 0 undefined behavior?

I'm having a disagreement with some co-workers over the following code: int foo ( int a, int b ) { return b > 0 ? a / b : a; } Does this code exhibit undefined behavior? EDIT: The disagreement started from what appears to be a bug in an…
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
60
votes
5 answers

Does `sizeof` *really* evaluate to a `std::size_t`? Can it?

Take the following standard passage: [C++11: 5.3.3/6]: The result of sizeof and sizeof... is a constant of type std::size_t. [ Note: std::size_t is defined in the standard header (18.2). —end note ] Now: [C++11: 18.2/6]: The type size_t…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
60
votes
1 answer

What does the standard say about how calling clear on a vector changes the capacity?

This website implies that clearing a vector MAY change the capacity: http://en.cppreference.com/w/cpp/container/vector/clear Many implementations will not release allocated memory after a call to clear(), effectively leaving the capacity() of the…
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91