Author Topic: Few questions about C++ syntax.  (Read 2507 times)

anphanax

  • Member
  • **
  • Posts: 197
  • Kudos: 11
    • http://june.tripod.com
Few questions about C++ syntax.
« 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...)
Quote

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)

muzzy

  • Member
  • **
  • Posts: 391
  • Kudos: 409
    • http://muzzy.net/
Re: Few questions about C++ syntax.
« Reply #1 on: 26 June 2005, 18:56 »
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

anphanax

  • Member
  • **
  • Posts: 197
  • Kudos: 11
    • http://june.tripod.com
Re: Few questions about C++ syntax.
« Reply #2 on: 26 June 2005, 20:10 »
I asked about the const &void because MSVC++ let me use it for some reason. Thanks for answering the questions :).

muzzy

  • Member
  • **
  • Posts: 391
  • Kudos: 409
    • http://muzzy.net/
Re: Few questions about C++ syntax.
« Reply #3 on: 26 June 2005, 20:38 »
it does let you use it? which version?

muzzy

  • Member
  • **
  • Posts: 391
  • Kudos: 409
    • http://muzzy.net/
Re: Few questions about C++ syntax.
« Reply #4 on: 26 June 2005, 20:40 »
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?

anphanax

  • Member
  • **
  • Posts: 197
  • Kudos: 11
    • http://june.tripod.com
Re: Few questions about C++ syntax.
« Reply #5 on: 27 June 2005, 01:39 »
This is where i'm using "const &void" (Version 7, untested with Version 6):

Quote

#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;
}

solemnwarning

  • Member
  • **
  • Posts: 747
  • Kudos: 338
    • http://www.solemnwarning.net
Re: Few questions about C++ syntax.
« Reply #6 on: 28 June 2005, 01:34 »
perl rules

Code: [Select]
#!/usr/bin/perl

$count = "1";

until($count == "0") {
  print "Never ending loop\n";
}
-----BEGIN GEEK CODE BLOCK-----
 Version: 3.1
 GCS/CM d- s+:+ a--- C++ UL++++>$ P+ L+++ !E W++ !N !o !K-- w !O !M !V PS+ PE- !Y !PGP !t !5 !X !R tv b+ DI+ !D G e- h !r y-
 ------END GEEK CODE BLOCK------

muzzy

  • Member
  • **
  • Posts: 391
  • Kudos: 409
    • http://muzzy.net/
Re: Few questions about C++ syntax.
« Reply #7 on: 28 June 2005, 06:03 »
Quote from: anphanax
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.

skyman8081

  • VIP
  • Member
  • ***
  • Posts: 910
  • Kudos: 187
    • http://sauron.game-host.org/
Re: Few questions about C++ syntax.
« Reply #8 on: 28 June 2005, 08:03 »
perl = bloated.

Observe, in python:
Code: [Select]
while True:
     print "Unending loop!"


if it has a .py extension, you don't need the #!/usr/bin/python statement.
2 motherfuckers have sigged me so far.  Fuck yeah!


muzzy

  • Member
  • **
  • Posts: 391
  • Kudos: 409
    • http://muzzy.net/
Re: Few questions about C++ syntax.
« Reply #9 on: 28 June 2005, 15:13 »
Quote from: skyman8081
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.