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
83
votes
6 answers

Is it allowed for a compiler to optimize away a local volatile variable?

Is the compiler allowed to optimize this (according to the C++17 standard): int fn() { volatile int x = 0; return x; } to this? int fn() { return 0; } If yes, why? If not, why not? Here's some thinking about this subject: current…
geza
  • 28,403
  • 6
  • 61
  • 135
83
votes
4 answers

How can this structure have sizeof == 0?

There is an old post asking for a construct for which sizeof would return 0. There are some high score answers from high reputation users saying that by the standard no type or variable can have sizeof 0. And I agree 100% with that. However there is…
bolov
  • 72,283
  • 15
  • 145
  • 224
82
votes
3 answers

Why is a const variable sometimes not required to be captured in a lambda?

Consider the following example: #include int main() { const int m = 42; [] { m; }(); // OK const int n = std::rand(); [] { n; }(); // error: 'n' is not captured } Why do I need to capture n in the second lambda but not m…
s3rvac
  • 9,301
  • 9
  • 46
  • 74
82
votes
5 answers

What are the valid signatures for C's main() function?

What really are the valid signatures for main function in C? I know: int main(int argc, char *argv[]) Are there other valid ones?
Prady
  • 10,978
  • 39
  • 124
  • 176
81
votes
7 answers

Can I use NULL as substitution for the value of 0?

Am I allowed to use the NULL pointer as replacement for the value of 0? Or is there anything wrong about that doing? Like, for example: int i = NULL; as replacement for: int i = 0; As experiment I compiled the following code: #include…
80
votes
10 answers

Why would the behavior of std::memcpy be undefined for objects that are not TriviallyCopyable?

From http://en.cppreference.com/w/cpp/string/byte/memcpy: If the objects are not TriviallyCopyable (e.g. scalars, arrays, C-compatible structs), the behavior is undefined. At my work, we have used std::memcpy for a long time to bitwise swap…
R Sahu
  • 204,454
  • 14
  • 159
  • 270
80
votes
4 answers

Different cast operator called by different compilers

Consider the following short C++ program: #include class B { public: operator bool() const { return false; } }; class B2 : public B { public: operator int() { return 5; } }; int main() { B2 b; …
buc
  • 6,268
  • 1
  • 34
  • 51
78
votes
1 answer

Why is 019 not a JavaScript syntax error? Or why is 019 > 020

If I type 019 > 020 in the JavaScript console (tested in both Chrome and Firefox), I get the answer true. This is due to 020 being interpreted as an OctalIntegerLiteral (equals 16) whereas 019 is apparently being interpreted as DecimalLiteral (and…
lexicore
  • 42,748
  • 17
  • 132
  • 221
78
votes
7 answers

Why was the space character not chosen for C++14 digit separators?

As of C++14, thanks to n3781 (which in itself does not answer this question) we may write code like the following: const int x = 1'234; // one thousand two hundred and thirty four The aim is to improve on code like this: const int y =…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
77
votes
2 answers

Cv-qualifications of prvalues (revisited)

This is a followup to my previous question, where the apparent consensus was that the change in treatment of cv-qualifications of prvalues was just a fairly minor and inconsequential change intended to solve some inconsistencies (e.g. functions…
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
76
votes
5 answers

Is masking before unsigned left shift in C/C++ too paranoid?

This question is motivated by me implementing cryptographic algorithms (e.g. SHA-1) in C/C++, writing portable platform-agnostic code, and thoroughly avoiding undefined behavior. Suppose that a standardized crypto algorithm asks you to implement…
Nayuki
  • 17,911
  • 6
  • 53
  • 80
76
votes
1 answer

Type of `this` in static member function?

In C++ 5.1.1/3 [expr.prim.general] it says: The type and value category [of this] are defined within a static member function. What does this mean? How is it relevant? Note that: this shall not appear in the declaration of a static member…
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
76
votes
4 answers

What is the behavior of printing NULL with printf's %s specifier?

Came across an interesting interview question: test 1: printf("test %s\n", NULL); printf("test %s\n", NULL); prints: test (null) test (null) test 2: printf("%s\n", NULL); printf("%s\n", NULL); prints Segmentation fault (core dumped) Though this…
Deepanjan Mazumdar
  • 1,447
  • 3
  • 13
  • 20
75
votes
2 answers

Why don't I need to specify "typename" before a dependent type in C++20?

This bit of code compiled in C++20 (using gcc 10.1) without using the typename keyword before the dependent type std::vector::iterator. Why does it compile? #include template std::vector::iterator // Why does this not…
Jake Schmidt
  • 1,558
  • 9
  • 16
75
votes
9 answers

How do I specify an integer literal of type unsigned char in C++?

I can specify an integer literal of type unsigned long as follows: const unsigned long example = 9UL; How do I do likewise for an unsigned char? const unsigned char example = 9U?; This is needed to avoid compiler warning: unsigned char example2 =…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295