3

I have a class, which extensively uses members of a particular namespace, like this:

class Entity {

    using namespace glm;

  public:

    Entity(vec3 position, vec3 direction, vec3 upVector, vec3 velocity, float speed = 0.0f);
    Entity(vec3 position, vec3 direction, vec3 upVector);
    Entity(vec3 position, vec3 direction);
    virtual ~Entity() {}

    vec3 position()   { return this->pos; }
    vec3 direction()  { return this->dir; }
    vec3 upVector()   { return this->upVec; }
    vec3 velocity()   { return this->vel; }
    float speed()     { return this->spd; }

    // lots of other methods

  protected:

    vec3 pos;
    vec3 dir;
    vec3 upVec;
    vec3 vel;
    float spd;

    // lots of other members

};

I just discovered using namespace is not allowed inside a class, so i can't do it this way. I can see only 2 options how to get out of this, both of which are stupid:

  1. repeat the namespace_name:: (glm::) before each usage of members (vec3, vec4, mat3, ...)
  2. declare using namespace outside of the class and force this namespace to everyone, who includes my header

Is there a nicer/cleaner way, how to solve this problem?

Youda008
  • 1,788
  • 1
  • 17
  • 35
  • use typedef? something like typedef glm::X glmX ? – alampada Nov 06 '15 at 12:04
  • 1
    using glm::vec3 inside the class would work – Tom Tanner Nov 06 '15 at 12:08
  • Why is 1 a problem? I'd say it's best practice and most readable. – Christian Hackl Nov 06 '15 at 15:17
  • I don't mind writing namespace, if there are only few cases of usage of it's members, but when there is a lot of it 1. After some time it starts to be really annoying to write it everywhere. 2. It looks ugly and expands the code to the right out of the screen. 3. glm library was meant to provide same tools as OpenGL Shading Language, which uses these things as vec3, mat3, ... without any prefix. – Youda008 Nov 07 '15 at 10:27

3 Answers3

4

As a matter of good practice your own class should be in a namespace too. You can put the using statement there.

namespace MyProject
{
    using namespace glm;

    class Entity
    {
        ...
    };
}
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
2

If you are are just going to use vec3 constantly, you can use a typedef:

class Entity {
public:
    typedef glm::vec3 vec3;

    Entity(vec3 position, vec3 direction, vec3 upVector, vec3 velocity, float speed = 0.0f);
    // more things...
};

Hope this helps.

Baltasarq
  • 12,014
  • 3
  • 38
  • 57
0

If you take a look at this previous question you may get some insight into ways around this problem. I believe most people are agreed that defining an unlimited scope namespace inside header files is a bad idea because it will impact every other file that includes your header. If the syntax is permitted, enclosing your class AND the namespace in another block would seem to be the best solution. The namespace scope would thereby be limited to your outer block. Unfortunately it's been a while since I did serious c++ work and I can't remember if this syntax will be allowed by the compiler in a header file:

{
using namespace glm;

class Entity {
    ...
}

}
Community
  • 1
  • 1
PeteB
  • 372
  • 1
  • 10