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

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.

Related

Is it okay to shorthand a function call using a macro? C++ [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 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
I am slowly writing an emulator for a gameboy using C++, I am currently working on the CPU side of things. I have written a generic function that takes any two registers of the CPU and returns as a Word data type. As it is necessary to access individual registers and also a combination of registers.
const Word get_word(Byte *registerOne, Byte *registerTwo)
{
return ((*registerOne << 8) | *registerTwo);
};
calling this function gets tedious as you have to specify each register
get_word(&this->registers.h, &this->registers.l)
My question is if it okay to define a macro like so
#define get_HL() get_word(&this->registers.h, &this->registers.l)
since now I can call it using
get_HL()
The reason why I want to do it like this since I don't want to create more private/public functions that just perform function calls.
I have tried compiling and it seems to work as it should since its just a pre-processor macro but I am not sure of the design implication
EDIT:
Okay I mean there are glaring flaws with this and you should just make a function, just as much work to make a function or write a macro.
const Word get_HL() { return this->get_word(&this->h, &this->l); };
Let this be a post for people who had the same idea and hopefully stop making the same mistake
No, this isn't OK in my opinion. It hides what arguments you're passing into the function, and macros don't respect scopes and as such are highly susceptible to name conflicts. This seems like an ideal use case for a non-static member function.

Meaning of these particular pointers [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 3 years ago.
Improve this question
I'm hesitant to consider this a valid question so close or downvote if you wish, I promise not to be offended :-)
I've just been watching Herb Sutter's excellent CppCon2018 talk on making C++ more powerful and simpler at the same time. I'd strongly suggest watching this if you haven't already.
Near the end, he references an xkcd cartoon on pointers, where the three pointers given are suspiciously ASCII in nature(a), usually a sure sign that you've somehow got yourself a corrupt pointer.
The three pointers are 0x3a28213a, 0x6339392c and 0x7363682e, which equate to the three character blocks :(!:, c99, and sch. (though endian issues may reverse the order of the characters).
Does anyone know if there's any significance to these pointers?
(a) Yes, I also noticed the ASCII in Homer Simpson's 3d episode sequence, meaning I've been in this game way too long :-)

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

Where to find what function names standfor? C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I am learning c++ and I keep running into a common problem: I cannot find the reasons behind function names.
For example, ifstream seekg() function. I attempt to look up the function on websites like:
http://www.cplusplus.com/reference/istream/istream/seekg/
https://en.wikipedia.org/wiki/Seekg
http://en.cppreference.com/w/cpp/io/basic_istream/seekg
Many times, none of these sources will give me a clue as to why it is named "seekg". Why is there a 'g' at the end? why does the ofstream have seekp (instead of g)?
Knowing that kind of information would make memorizing function names so much easier. Essentially, I'm looking for a resource for finding the etymology for function names. :)
Thanks for any help,
I can answer the specific question here. seekg refers to seek implemented over get area, while seekp works on put area.
The reason for the distinction is that streams support to separate areas - one is for reading, commonly refered to as source (get area), and another for writing, known as sink (put area). Quite often one works with streams which are limited to only one of those - std::ifstream only has get area, while std::ofstream has put area - but occasionally you deal with both.

C++ - Best Practice: `using std::cout` vs `std::cout` [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 5 years ago.
Improve this question
I understand that, in C++, we should never use:
using namespace std;
The two possible alternatives are:
1) adding using std::cout; at the beginning of the file and just type cout whenever required
2) type std::cout every time we need to use cout
My understanding is that the second method is the best. However, is this always followed in professional environments? Is it practical to follow in highly fast paced environments? I am used to the first alternative. Is it an advantage to switch?
Note: I originally posted this in Code Review and I was told that this topic belonged here. Kindly let me know if not.
So I have done a little bit with C++, but I would say the problem falls under the problem of namespaces in all languages. The real problem is that if you have multiple namespaces with the same function, it makes it much more difficult to read whats going on and can cause undesired results.
For example if you have two functions with the same name in two name spaces, how will the code know which one to use? Also another problem comes when you have multiple namespaces added and call a function. How does someone reading the code know which namespace the code is coming from? Having the namespace in front of the function helps make your code more readable.