POV-Ray : Newsgroups : povray.programming : Vectors in C++ : Re: Vectors in C++ Server Time
6 Oct 2024 13:47:33 EDT (-0400)
  Re: Vectors in C++  
From: Warp
Date: 17 Dec 2002 04:59:04
Message: <3dfef567@news.povray.org>
Tony[B] <ben### [at] catholicorg> wrote:
> Just to confirm something, Warp. This

> std::vector<double> myVector(3);

> is instantiating a double vector, with an initial size of 3 elements, yes?

  Yes.

  You can also give the initial value of the three elements as a second
parameter, but as the default constructor for 'double' initializes them
to 0, it's probably not necessary.

> Also, do you prefer to always add "std::", or do you think it's ok to toss
> in the respective "using"? (Say, as long as this file is not to be included
> in another.)

  It depends.
  If the 'std::vector<double>' definition is used just a couple of times
in the file, then it's not too much trouble to write the 'std::' prefix.
If it's used tens of times, then I might write "using std::vector;" to
pop it out of the namespace.

> And on that topic, is there, or wouldn't it be neat if there
> were, something like "stop using", so you could do things like this?

> using namespace std;

> /* do your thing */

> stop using namespace std;

  Well, this is actually possible indirectly.
  If you write that inside a function (or any {}-block), its scope will end
at the end of the function (or {}-block) as well. That is:

void f()
{
    using namespace std:

    ...(some code here)...

    // using namespace std ends here
}

  These are the programming guidelines about 'using' which I have learned:

  1. As a general rule, don't use 'using'.

  2. If it would be a lot of work to write all the 'std::' prefixes, put
     a "using std::whatever;" inside the function where you are using the
     specified type.

  3. If there would be lots of "using std::whatever;" lines at the beginning
     of the function (eg. more than five) then you might consider writing
     "using namespace std;" instead in the function.

  4. If there are lots of functions in the .cpp file and most of them would
     have the same "using std::whatever;" lines, then you might want to
     write them just once at the beginning of the file.

  5. And again, if there would be lots of those lines (but in this case there
     would be a lot more, eg. 10 or more), then you might consider writing
     "using namespace std;" at the beginning of the file. However, if it's
     not too much trouble, try avoiding this.

  6. And the most important rule of all: Never ever ever use a 'using'
     statement in a header file. Never. Period.

  Note to rule 6: You might think that if you are implementing an inline
function in a header file, it may be ok to write 'using' inside that function.
However, this is still not the case: If the implementation of that function
is really so large that it contains lots and lots of 'std::' prefixes, then
it's probably too long to be a good inline function in the first place.

-- 
#macro M(A,N,D,L)plane{-z,-9pigment{mandel L*9translate N color_map{[0rgb x]
[1rgb 9]}scale<D,D*3D>*1e3}rotate y*A*8}#end M(-3<1.206434.28623>70,7)M(
-1<.7438.1795>1,20)M(1<.77595.13699>30,20)M(3<.75923.07145>80,99)// - Warp -


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.