Where did the function and the variable come from? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
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++...

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.

Related

C++ operator overloading for embedded DSL language in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Is it possible to use C++ operator overloading and create a DSL-like syntax for embedded DsL code in c++.
"a": auto = call("add2Numbers", "b", "c");
This is what I would like ideally. But anything close to this as valid C++ would be acceptable.
Good advice: don't. C++ is a big multi purpose language and already complicated enough. You will confuse people (and yourself too!) if you randomly change stuff. Especially operator overloading and the proposed pre processor should be treated carefully.
My advice for you is to write the functions you need. You gave an example about some kind of assignment (sorry, don't understand the given code) and I'm sure it is perfectly possible to write a function with a convenient interface to achieve what you're trying to do. The advantage is that your synthax stays all C++ and possible readers (and again your future self) don't get confused.
I hope I understood your question properly. If not, please correct me.
Felix

c++ cout vs printf() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
After learning about c++ through a couple different sources, I found conflicting advice concerning the use of cout/printf(). One source said that printf(), and I quote:
... does not provide type safety, so it is easy to inadvertently tell it to display an integer as if it were a character and vice versa. printf() also does not support classes, and so it is not possible to teach it how to print your class data; you must feed each class member to printf() one by one.
So, the more important thing to me would be the readability factor of using printf(). On the other hand, another source mentioned that cout, with its use of the overloaded operator <<, uses more instructions to execute and can therefore be more expensive in terms of memory over a large program. Although, the person who said this was a systems programmer, in which every bit of performance is vital. But say I wanted to go into game or application development.
Would the performance differences between printf() and cout matter all that much?
In general, does it really matter what I choose to use in an application program?
Thank you for any input.
You would measure the differences on your particular implementation for your specific use case and determine that for yourself.
I would say both lines of reasoning in the question have merit, but you can't generalise about performance.
If you want to go into game or application programming, printf/cout won't be needed too much. The only use in these cases is for debugging.
If you really need to use printf/cout a lot, the difference will be when writing a huge amount of data, otherwise you don't need to bother.

About the "Reference Arguments" rule in Google C++ Style [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm converting my code to follow the google C++ style guide. The Reference Arguments rule says "All parameters passed by reference must be labeled const" and "input arguments are values or const references while output arguments are pointers".
With respect to the signature void MyTable::LoadTable(ifstream &fin), how can I label the parameter fin const given LoadTable invokes some non-const function of fin, e.g. fin.seekg? I think fin should be regarded as an input/output parameter thus it's a little bit special. What will googlers do in this situation?
UPDATE: I knew there are lots of criticisms about the google style guide. I just wondered how googlers tackle it, and maybe I found an answer: there is another rule Streams reading "Use streams only for logging". Maybe they just don't use streams in this situation.
In order to comply with Google guidelines, change the declaration
void MyTable::LoadTable( ifstream& fin )
to
void MyTable::LoadTable( ifstream* fin )
Now you might be wondering, what's the point of that? And a large part of the answer is probably that the Google C++ style guide simply doesn't make much sense outside of Google. Parts of it can be explained by postulating the need to maintain a large body of C style legacy code, but parts of it are just baffling.
As just one example, std::getline is very much in breach of the Google style guidelines.

How to approach a C++ parser [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am wanting to have a go at a C++ parser for a formatter I am making.
You can obviously open a file and use getline(..) or get(), is this reasonable way of starting things off and then working out a system using vector arrays and hence creating loads of arrays and somehow structuring out and processing what you are doing from there. For example say I wanted to find ever function in a source file, all functions have the common syntax, "(){" once whitespace has been removed, so do you just look for common delimeters to parse out the sections into arrays. I suppose I will learn as I go.
Or I also assume there are tried and tested ways of doing this, and I would likley just be reinventing the wheel as they say.
C++ is a language that is quite hard to parse in the first place. So if you want anything other that really trivial C++ code to be "understood" by your parser, you are definitely better off starting with an existing product.
The Clang frontend library would perhaps be a good starting point.
There are also a number of "source to source" conversion examples based on clang. Here's one of them: http://eli.thegreenplace.net/2012/06/08/basic-source-to-source-transformation-with-clang/

Style and Enumerators [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
So, in the process of taking a data-structures class (in C++), my professor wanted us to manipulate a linked list of playing cards. That doesn't matter however, what I am interested in is why she did not use an enumerator to represent the suites.
In her code, she used strings to hold the suite of a card. This seemed inefficient because she wanted us to sort them based on suite, under the circumstances, it would have been considerably easier if she had used an enumerated type instead of a string. The string did not offer any help either, because in printing the suite, she output a Unicode character, roughly doubling the length of her function, simply because she did not use an enum.
Is there any reason for her to have done this, or does she simply have strange preferences when it comes to code style?
If you really want to know what your professor's reasoning is, you have to ask your professor. I can only speculate.
But if I were to speculate, I would guess that there are two possible reasons why your professor chose to use strings as descriptors for these attributes.
She is trying to keep the code simple and easy for newbie C++ programmers to understand. Whether the means meet the goal is debateable.
(Personal bias alert) Professors and others in academia, with no real-world experience, often do and teach things that I would consider to be highly sub-optimal.
My guess would be that she either had not considered that approach or that she wanted to test your ability to work with sorting strings.
Code examples might help in that they might clarify what she did and what you think she should have done.
The likely answer is that she just didn't think about the problem she was using to demonstrate whatever she is trying to teach you. That is, she wanted you to focus on sorting (for example), and probably took some code written by someone else and just adapted it to that purpose without much thought.