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
122
votes
9 answers

When using C headers in C++, should we use functions from std:: or the global namespace?

C is somewhat, not exactly, a subset of C++. So we can use most of the C functions/headers in C++ by changing the name a little bit (stdio.h to cstdio, stdlib.h to cstdlib). My question is actually kind of semantic. In C++ code (using newest…
DeiDei
  • 10,205
  • 6
  • 55
  • 80
120
votes
5 answers

Why does the ternary operator with commas evaluate only one expression in the true case?

I'm currently learning C++ with the book C++ Primer and one of the exercises in the book is: Explain what the following expression does: someValue ? ++x, ++y : --x, --y What do we know? We know that the ternary operator has a higher precedence…
117
votes
2 answers

Program being compiled differently in 3 major C++ compilers. Which one is right?

As an interesting follow-up (not of big practical importance though) to my previous question: Why does C++ allow us to surround the variable name in parentheses when declaring a variable? I found out that combining the declaration in parentheses…
Predelnik
  • 5,066
  • 2
  • 24
  • 36
114
votes
3 answers

Is passing a C++ object into its own constructor legal?

I am surprised to accidentally discover that the following works: #include int main(int argc, char** argv) { struct Foo { Foo(Foo& bar) { std::cout << &bar << std::endl; } }; Foo foo(foo); // I can't…
Andrew Wagner
  • 22,677
  • 21
  • 86
  • 100
113
votes
3 answers

What is the difference between C++03 `throw()` specifier and C++11 `noexcept`?

Is there any difference between throw() and noexcept other than being checked at runtime and compile time respectively? This Wikipedia C++11 article suggests that the C++03 throw specifiers are deprecated. Why so ... Is the noexcept capable enough…
iammilind
  • 68,093
  • 33
  • 169
  • 336
112
votes
2 answers

Is it still safe to delete nullptr in c++0x?

In c++03 it is pretty clear that deleting a null pointer has no effect. Indeed, it is explicitly stated in §5.3.5/2 that: In either alternative, if the value of the operand of delete is the null pointer the operation has no effect. However, in the…
Mankarse
  • 39,818
  • 11
  • 97
  • 141
110
votes
8 answers

Efficient unsigned-to-signed cast avoiding implementation-defined behavior

I want to define a function that takes an unsigned int as argument and returns an int congruent modulo UINT_MAX+1 to the argument. A first attempt might look like this: int unsigned_to_signed(unsigned n) { return static_cast(n); } But as…
Nemo
  • 70,042
  • 10
  • 116
  • 153
109
votes
6 answers

Why is "using namespace X;" not allowed at class/struct level?

class C { using namespace std; // error }; namespace N { using namespace std; // ok } int main () { using namespace std; // ok } I want to know the motivation behind it.
iammilind
  • 68,093
  • 33
  • 169
  • 336
109
votes
2 answers

Does C++20 mandate source code being stored in files?

A slightly strange question, however, if I remember correctly, C++ source code doesn't require a file system to store its files. Having a compiler that scans handwritten papers via a camera would be a conforming implementation. Although practically…
JVApen
  • 11,008
  • 5
  • 31
  • 67
106
votes
7 answers

Confusion about array initialization in C

In C language, if initialize an array like this: int a[5] = {1,2}; then all the elements of the array that are not initialized explicitly will be initialized implicitly with zeros. But, if I initialize an array like this: int…
msc
  • 33,420
  • 29
  • 119
  • 214
105
votes
5 answers

C++ - Why static member function can't be created with 'const' qualifier

Today I got a problem. I am in the need of a static member function, const is not a must but a better. But, I didn't succeed in my efforts. Can anybody say why or how?
prabhakaran
  • 5,126
  • 17
  • 71
  • 107
105
votes
9 answers

Does "Undefined Behavior" really permit *anything* to happen?

The classic apocryphal example of "undefined behavior" is, of course, "nasal demons" — a physical impossibility, regardless of what the C and C++ standards permit. Because the C and C++ communities tend to put such an emphasis on the…
Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
104
votes
9 answers

Have there ever been silent behavior changes in C++ with new standard versions?

(I'm looking for an example or two to prove the point, not a list.) Has it ever been the case that a change in the C++ standard (e.g. from 98 to 11, 11 to 14 etc.) changed the behavior of existing, well-formed, defined-behavior user code - silently?…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
103
votes
2 answers

Effectively final vs final - Different behavior

So far I thought that effectively final and final are more or less equivalent and that the JLS would treat them similar if not identical in the actual behavior. Then I found this contrived scenario: final int a = 97; System.out.println(true ? a :…
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
103
votes
2 answers

Is 'auto const' and 'const auto' the same?

Is there a semantic difference between auto const and const auto, or do they mean the same thing?
steffen
  • 8,572
  • 11
  • 52
  • 90