Miscellaneous > Programming & Networking

Few questions about C++ syntax.

(1/2) > >>

anphanax:
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)

--- End quote ---

muzzy:
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:
I asked about the const &void because MSVC++ let me use it for some reason. Thanks for answering the questions :).

muzzy:
it does let you use it? which version?

muzzy:
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?

Navigation

[0] Message Index

[#] Next page

Go to full version