7

In a GoogleTechTalks video on Youtube, Bjarne Stroustrup talks about the upcoming C++0x standard. In the video he mentions the following example:

#include <iostream>

struct Sick
{
    Sick(double d)       { std::cout << d << "\n"; }
    explicit Sick(int i) { std::cout << i << "\n"; }
};


int main()
{
    Sick s1 = 2.1;
    Sick s2(2.1);
}

Did he mean to place the explicit keyword before Sick(double) rather than Sick(int), in order to highlight problems associated with implicit conversions in certain contexts?

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
Truncheon
  • 916
  • 2
  • 12
  • 25
  • 3
    How are we supposed to know what someone in a youtube video meant? The important thing is that *you* understand the use of `explicit` in this example. – Kerrek SB Sep 19 '11 at 18:39
  • 2
    Either would be reasonable, depending on what you want to show with the example. – Flexo Sep 19 '11 at 18:41
  • 3
    @Kerrek SB, You're suppose to know what he meant because he's trying to communicate what he meant. – ikegami Sep 19 '11 at 18:53
  • 7
    @ikdegami: the example doesn't even use the `int` version, so I highly doubt that the example is accurately reproduced or relevant to the point that was to be made. The extraneous mention of C++11 further reinforces that there's a disconnect between the video talk and the OP's question. – Kerrek SB Sep 19 '11 at 18:56
  • 2
    Guys, don't take this too seriously. I'm just curious as to why the explicit keyword was placed where it is, when it really doesn't seem to be needed. Trying compiling the example as is, and then remove the keyword. Also, I'm quite sure Mr S made a simple mistake as we all do from time to time. – Truncheon Sep 19 '11 at 19:00
  • 2
    @Truncheon: I don't know if this question is useful. Your example never uses a constructor from `int`, so it's totally irrelevant whether you declare it explicit or not. If you have a question about the `explicit` keyword, it's best to ask that directly, or otherwise rewatch the video to see if you didn't misunderstand something. For what it's worth, C++11 adds new explicit *conversion* operators, maybe that's something you want to look into. – Kerrek SB Sep 19 '11 at 19:08
  • @Kerrek, the title of the talk is "C++0x Initialization: Lists," so the C++11 tag is not extraneous, even though this particular example code is meant to demonstrate a shortcoming of previous C++ editions. You don't have to just "doubt" whether the example was accurately reproduced. Go look at the video yourself, which I think is what Truncheon expected people to do before answering. And when you question whether the example is accurate, that's the point! The question asks whether it was Truncheon who misunderstood something, or Stroustrup who made a mistake. (The latter, as it turns out.) – Rob Kennedy Sep 19 '11 at 20:28

1 Answers1

9

In his discussion, Stroustrup mentions that a direct initialization, such as

Sick s2(2.1);

will consider only constructors marked explicit if there are any explicit constructors. That's not my experience with several compilers (including GCC 4.6.1 and MSVC 16/VS 2010), and I can't find such a requirement in the standard (though I'd be interested if someone can point it out).

However, if ints are used in the initializers, I think the example would show what Stroustrup meant to demonstrate:

#include <iostream>

struct Sick
{
    Sick(double d)       { std::cout << "double " << d << "\n"; }
    explicit Sick(int i) { std::cout << "int " << i << "\n"; }
};


int main()
{
    Sick s1 = 2;
    Sick s2(2);
}

Running the above will display:

double 2
int 2

Shows that the two apparently equivalent initializations actually select different constructors.

(Or as Truncheon mentioned in the question - and I missed - that the explicit keyword should be on the Sick(double d) constructor).

Michael Burr
  • 333,147
  • 50
  • 533
  • 760