-2

Please sorry, I am JavaScript and TypeScript guy, not a c++ one.

But the JS engine V8 is written in c++ and here is a code piece from there:

// Convert the result to an UTF8 string and print it.
v8::String::Utf8Value utf8(isolate, result);
printf("%s\n", *utf8);

In the code above there are two lines. First line contains utf8 function... where does it come from? I didn't see it before in the file and it wasn't imported (or was it)?

Second line contains utf8 variable (right?), though with * modifier which I am not aware of. Where did the variable come from? What is the role of the star modifier?

Sorry for this kind of questions, but at this point I cannot delve into the documentation of one of the most complex languages, which is c++...

Nurbol Alpysbayev
  • 19,522
  • 3
  • 54
  • 89
  • The first line *declares* `utf8`, it introduces a new identifier. So it's normal that you never encounter it earlier in the file. The second line then uses it. These are rather basic and fundamental concepts of C++, and you would better off reading a good book to learn the language. C++ is not a language that can easily be figured out on your own. For all it's strengths it's a very unforgiving and at time unintuitive language. – François Andrieux May 25 '19 at 23:27
  • how explaining two lines of code help you in the bigger picture? Sure you might get a sense about these two lines of code but that is not going to magically make you understand C++. So are you going to ask for help for every line of C++ you see? I think a better approach for you is to start a quick intro into C++.You can start with something like [c++ for javascript programmers](https://www.google.com/search?q=c%2B%2B+for+javascript+programmers) – bolov May 25 '19 at 23:28
  • @bolov Thanks for the advice and link! Let's say I am evaluating if it is worth digging deeper by trying to figure out what this simple hello world sample does. – Nurbol Alpysbayev May 25 '19 at 23:30
  • The first line does not contain a function, and `*` is an operator, not a "modifier" (what do you mean by that?). – melpomene May 25 '19 at 23:31
  • @FrançoisAndrieux Thanks! So a variable declaration can look like a function call expression? Am I right? – Nurbol Alpysbayev May 25 '19 at 23:31
  • No, function calls cannot look like that. There is an ambiguity in the grammar between variable declarations and function declarations, but not function calls. – melpomene May 25 '19 at 23:32
  • @melpomene Thanks for the correction, I knew this must be wrong term. So first line contains a declaration that looks like a function call expression? – Nurbol Alpysbayev May 25 '19 at 23:33
  • 2
    No, it does not look like a function call expression. – melpomene May 25 '19 at 23:33
  • @NurbolAlpysbayev `v8::String::Utf8Value utf8(isolate, result);` doesn't look like a function call expression. Maybe `utf8(isolate, result);` or `v8::String::Utf8Value foo = utf8(isolate, result);` look like function call expressions, if `utf8` was a declared function. – François Andrieux May 26 '19 at 00:26
  • To future reviewers: I misclicked Leave Open, sorry. I intended to click Close. I have voted to close. – L. F. May 26 '19 at 01:14

1 Answers1

6

utf8 is not a function, but a variable. The snippet (isolate, result) is the arguments passed to its constructor.

This could be rewritten as follows to be functionally identical, and in a way that is more familiar to a JavaScript programmer:

auto utf8 = v8::String::Utf8Value(isolate, result);

where auto infers the type of a variable.

As for the * in *utf8, the meaning of this will depend the implementation. * as a prefix operator can be given a user-defined meaning, though usually it has the semantics of "reach into and get the value from," as with raw pointers and things like std::unique_ptr and std::optional. I'm not familiar with v8 personally. You should look for documentation on the * operator for the v8::String::Utf8Value type, to see exactly what it does.

You should also be very aware that C++ takes a long time to learn, and it's terribly easy to misunderstand or do things very wrong. If you want to commit to learning C++, I would suggest reading a good C++ book to get a foundational understanding.

alter_igel
  • 6,899
  • 3
  • 21
  • 40
  • Wow. This kind of user-defined behavior makes me totally agree with your last paragraph. Thank you so much, for the answer and for the link – Nurbol Alpysbayev May 25 '19 at 23:38