5

I managed to track a bug down to the following expression:

foo(static_cast<T>(a, b)); // Executes specialisation 1

The closing bracket was in the wrong place. The correct statement should have been:

foo(static_cast<T>(a), b); // Executes specialisation 2

I've never seen static_cast used with the form (a,b), or seen it described anywhere. What does it mean? The former statement returned b.

Robinson
  • 9,666
  • 16
  • 71
  • 115
  • It's just a bug, not a special case of a static_cast. It's the comma operator, doing its thing. – Joe May 29 '15 at 14:08

3 Answers3

15

static_cast is not a function, it's a keyword, so the comma in a, b is not an argument separator; it is the comma operator. It evaluates a but throws away the result. The expression evaluates to b.

Community
  • 1
  • 1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
8

This has nothing to do with static_cast, but "makes use" of the comma operator. Its result is its right hand side, so

foo(static_cast<T>(a, b));

is equivalent to

foo(static_cast<T>(b));

unless a has other effects (which would then be executed and have their result discarded). With the right compiler settings, you will be warned about such things: Live

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
  • Why is ""makes use"" in quotation marks like you're being ironic or hiding a secret meaning? – Kerrek SB May 29 '15 at 14:11
  • @KerrekSB "Makes use" in the sense "No one actually intended to use this", i.e. no one *actually* makes use of it, they just happened to invoke it on accident. – Baum mit Augen May 29 '15 at 14:12
0

, is comma operator, so a, b is just b

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Xiaotian Pei
  • 3,210
  • 21
  • 40