Results 1 to 6 of 6

Thread: using directive and size_t in loop for

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default using directive and size_t in loop for

    Hello,
    I have this 3 question:
    1. I wonder what happen in a class declaration if I do using std::cout; and after cout << "hello"; or if I do std::cout << "Hello"; and more in general: is there any drawback to using "using std::mynamespace" instead of "using namespace mynamespace;" ?

    2. .NET compiler gives warning on for(int i=0; i<vec.size();.............) then I use for (size_t i=0............) because "size()", I guess, return a size_t; but is size_t portable? or what is better? i < static_cast<int> (vec.size()) ? Or C -style cast, (int)

    3. I've forgotten the third question right now........

    Thanks...
    Regards

  2. #2
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: using directive and size_t in loop for

    1. You can use:
    using namespace std or
    using std::cout

    In both cases you can use either 'cout' or 'std::cout'. However, if you are missing such a directive, you need to use 'std::cout'.

    The drawback of the 'using std::cout' directive is that you still have to specify the namespace for all symbols you use except cout (like endl).

    For anything other than really small programs, I am a fan of the 'using std::symbol' directive, because you don't pollute the local namespace with symbols you will never use.

    2. I don't understand the need of libraries to define their own basic types like this. It's as if the underlying type could someday change without warning. ;-)

    Anyway, in this case, size_t is likely an unsigned int, since the size of a vector can never be negative. If the compiler still complains, I would use size_t. I suppose it does give some small measure of extra portability.

    3. Well, be sure to tell us when you remember, ok?
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: using directive and size_t in loop for

    Quote Originally Posted by Michiel View Post
    It's as if the underlying type could someday change without warning. ;-)
    That's exactly the case. size_t can be different on different architectures, including 32b and 64b.

  4. #4
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: using directive and size_t in loop for

    Heh, I knew that statement was gonna come back and bite me in the a3.

    I'm designing a programming language now with (among other things) a single int-type, which is sized correctly at compile-time, by trying to find the actual limits in the code (or using a dynamic int if this is impossible). That might remove the need for such things.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: using directive and size_t in loop for

    Hello,
    1. I meant some drawback much more important. So I guess there aren't
    2. I don't sure to understand: apart 32b or 64b, I wonder even if could be different compiling my code with different compiler (not only MS compiler).....Does Anyone have this warning? How do you solve it? (I mean, the most elegant way.....)
    3.(Finally) I read that in the .h it's better don't allocate memory: eg. if I have a class where inside a "vector<double>* ivec" (Beyond if is better take the vector on the stack instead on the heap....); it's better call the "ivec = new vector<int>(10000);" not in a inlined constructor but keep it out of the .h, but in the implementation (.cpp). But is there any performance/compiling reasons (maybe something will change in the .obj file?) ?

    thanks.
    Regards

  6. #6
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: using directive and size_t in loop for

    1. No, there aren't.
    2. I get a "comparing signed with unsigned integer" warning when I use int. The solution is to use unsigned int. If that doesn't work for you, it's MS specific.
    3. There are no performance reasons for putting the constructor code anywhere specific, as far as I know. It's just that if you want to change the 10000 to 20000, you will have to recompile every file that includes your header. Whereas if you put it in your .cc/.cpp file, you'll only have to recompile that one.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.