Stop Microsoft
Miscellaneous => Programming & Networking => Topic started by: anphanax on 26 June 2005, 05:37
-
One:
Some people and I were talking in IRC about infinite loops, and we go into sort of an argument over: while(1) vs. for(;;). Does GCC optimize both of those? I've heard MSVC++ does.
Two:
What is the logic behind for(;;)? A particular user told me that while(1) wouldn't make sense to people, and my question was how does "for(;;)"? I would think if someone left the condition block of the for block blank, that it would be treated as false, and the loop would never execute, but w/e (NULL condition, NULL is 0, 0 is false).
Three:
Is this supposed to work? const &void = ...
(and, what's the point of it if it's legal)
Four:
Is this a good way to determine if a variable is of a certian type? I'm a bit worried about classes and inheritance with this method (I came up with the code myself, hence why it scares me -_-):
from .h file (i'm not gonna waste time creating a cpp file for the implementation details when they're either return true or false...)
template
class type_comparer
{
public:
static bool is_type(const matchtype var)
{
return true;
}
template
static bool is_type(const nottype var)
{
return false;
}
private:
type_comparer(void) {};
};
#define istype(type,variable) type_comparer::is_type(variable)
-
1) while(1) and for(;;) are technically equivalent. Sane compilers should optimize them the same.
2) the condition is optional, and if omitted, it implies it's always true. that's how it's specified
3) not legal. "void" is treated as name in that context, but it's a type-specifier it won't work. even if it was a valid name, the standard won't allow declarations without type.
4) i suppose something like that owrks, but templates are always compile-time tests and as of such it probably doesn't do what you want it to do. read about dynamic_cast<> and see if it's what you need.
edit: goddamnit ;) in for(;;) turning into a smiley
-
I asked about the const &void because MSVC++ let me use it for some reason. Thanks for answering the questions :).
-
it does let you use it? which version?
-
I just tested it with MSVC 7.1 and MSVC 6.0, and neither considers it valid. What's the exact code that works for you?
-
This is where i'm using "const &void" (Version 7, untested with Version 6):
#include
using namespace std;
int _cdecl main(int carg, char varg[])
{
const &void f = (void)main;
const &void a = (const &void)"This sure does make sense.";
char* b = (char*)&a;
puts(b);
return 0;
}
-
perl rules
#!/usr/bin/perl
$count = "1";
until($count == "0") {
print "Never ending loop\n";
}
-
This is where i'm using "const &void" (Version 7, untested with Version 6):
Now that's completely different from what you typed above, because this time you actually have a name there. However, it's still invalid. It appears MSVC parses it as const int &, merely ignoring the void specifier with a warning. It should damn be an error and not a warning, IMO.
-
perl = bloated.
Observe, in python:
while True:
print "Unending loop!"
if it has a .py extension, you don't need the #!/usr/bin/python statement.
-
if it has a .py extension, you don't need the #!/usr/bin/python statement.
Wrong. It won't find the interpreter at all if it doesn't have #! in the beginning... unless you're using windows and it's running python interpreter by file association, in which case perl doesn't need it either.