This question already has answers here:
Why does C++ allow us to surround the variable name in parentheses when declaring a variable?
(2 answers)
Closed 5 years ago.
I am new to C++, I see following syntax in c++ to initialize variable.
int(i)=1;
Then, I have compiled in G++ compiler and compiler did not give any error or warning.
So, What does mean int(i)=1; in C and C++?
Also, I have tested in C, I thought, the C compiler give an error but it's also working fine.
It's basically a strange way to write
int i = 1;
Nothing to worry about.
Sometimes, parenthesis around the variable name are necessary in defintions (eg. pointer to functions), and there is no reason to prohibit them for other cases, so it's allowed without any deeper reason.
Maythe the author didn't like spaces (such people exist).
Related
This question already has answers here:
Disable variable-length automatic arrays in gcc
(2 answers)
In C++ books, array bound must be constant expression, but why the following code works?
(2 answers)
Closed 4 years ago.
I try to understand, what happens when creating an array of unknown size on stack at compile time. Let consider this code:
int main()
{
int x;
cin >> x;
int tab[x];
}
I found a lot of information about this saying that you can not create an array of unknown size on the stack, but I didn't find any information why does the C++ compiler allows it, or maybe some of them do? What happens when creating such array? Is it even created on stack or already on heap?
Does the GCC compiler have some option to turn on, thanks to which such a constructions would be considered as errors or at least warnings?
C++ does not permit Variable Length Arrays (VLAs).
However, the most recent C standard does, so it can sometimes be found as an extension, such as it is with the GCC.
When compiling, make sure so explicitly select a language (chose C++17 or later if you can) and ask for pedantic (strictly standards-conforming) behavior.
This question already has answers here:
Why cast unused return values to void?
(10 answers)
Closed 6 years ago.
I just ran across this in some sample code and I've never seen it used before. For an inline function which returns a type but the return value is not used, the author preceded the call with a (void). Does this actually do anything?
Example:
inline some_object& SomeClass::doSomething();
SomeClass o;
(void)o.doSomething();
This is typically done when using a tool like Lint which has been configured to issue a warning if you call a function and ignore its return value.
This is (IMO) a horrible practice that's fostered by some tools1 that give warnings about calling a function and ignoring what it returns.
The right way to deal with the problem is to give the tool a list of functions whose return values can reasonably be ignored. If the tool doesn't support that, it's probably useless and should be thrown away. In the case of a compiler, you may not be able to throw away the tool itself, and may have to settle for just globally disabling that warning.
1. Most often something like lint, but some compilers can do the same.
This question already has answers here:
Why cast an unused function parameter value to void?
(2 answers)
Closed 8 years ago.
I saw following code few times
void func(SomeTypeEGInt varname) {
(void)varname;
}
I wish to know what it means and why people implement such functions.
It tell the compiler that those variables are unused. It is used to prevent the warnings which you will get.
The (void)varname; pattern is typically used to silence compiler warning about unused arguments. So this example is actually an empty function which does nothing.
This question already has answers here:
How to disable GCC warnings for a few lines of code
(9 answers)
How to suppress GCC warnings from library headers?
(10 answers)
Closed 9 years ago.
Adding the -Weffc++ flag already caught 2 real bugs in my code, so I'd like to leave it in. Unfortunately it leads to the following:
record-set.h:60:7: warning: ‘class RecordSet’ has pointer data members [-Weffc++]
record-set.h:60:7: warning: but does not override ‘RecordSet(const RecordSet&)’ [-Weffc++]
record-set.h:60:7: warning: or ‘operator=(const RecordSet&)’ [-Weffc++]
The warning is accurate. A RecordSet is basically a subset of a std::vector< Record > matching a rule. (I actually point to a data structure containing that, and a definition of what fields a Record has.) It has pointer data members because when I update a record, I need to update the original.
https://github.com/c42f/tinyformat/pull/4 offers the idea of just declaring the necessary functions to be private, then not use them. Unfortunately I do things like return a RecordSet from a function, so I need the copy constructor to actually exist with the default behavior.
The ideal would be to find some sort of inline comment that would tell gcc that yes, really, I want an exception here but please warn me about anything else that goes wrong.
Next best would be to write my own versions of those two functions that does exactly what the default does. I'm unfortunately not confident in my ability to get those exactly right. Can someone point me to a canonical example?
Easiest, of course, is to turn this flag off. But given that it caught real bugs for me, I would like to avoid that.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
why a[n] is accepted in c during runtime?
Declaring the array size with a non-constant variable
I just wrote some code to test some other code and I needed an array as input data. As the size of the input data may differ, I declared the variable as follows:
float input[num_pixels_row][num_pixels_col][3];
where num_pixels_row and num_pixels_col are non-const variables which are set using input from the command line. I ran the code and it worked.
Then after a little while I noticed what I had just done and thought "Hey, wait a minute! This shouldn't work!!" But the strange thing is that it does. Since input is declared inside a function it should be allocated on the stack, but how can the compiler determine the stack frame if the size of the array isn't known?
I asked two colleagues and they were just as puzzled. By the way, I compiled the code using g++ 4.6.1
That's a gcc-specific compiler extension which makes your code sub-standard and nonportable. For example, this won't compile in Visual C++.
Variable length arrays are a part of the C99 specification, which gcc also allows in C++ programs.
I don't think this has been added to C++11 though, unfortunately. Though I'd suspect that since many C++ compilers also strive for C compliance that they'll end up supporting this as well.