3

Suppose I have a very long namespace, which I don't want to type in all the time. But I don't want to use using namespace ... either. Can I use #define for this?

Example:

#define glm::quat glm::gtc::quaternion

class Camera
{
protected:
    glm::quat m_mRotation;
};

I don't want to use the using, because the glm:: part will be gone as well, I think. So I would like to keep the glm part, but not the totally long gtc::quaternion part. When I try this now, I get the following error:

Error 1 error C2008: ':' : unexpected in macro definition

Marnix
  • 6,384
  • 4
  • 43
  • 78

2 Answers2

13

you don't need a define, use a namespace alias:

namespace glm_quat = glm::gtc::quaternion::quat;

or if you want the alias to be really in the glm namespace, put it there:

namespace glm {
  namespace quat = gtc::quaternion::quat;
}
DaVinci
  • 1,391
  • 2
  • 12
  • 27
  • 1
    @DaVince: The glm namepspace is not mine, so that will never work, but I didn't know about the aliasing. Can I also use this statement in a `using namespace`? – Marnix Feb 20 '11 at 12:27
  • @Mamix: Yes, you can use this in a `using namespace` so it doesn't matter if the namespace isn't yours. The only thing DaVince's was wrong about is that this is namespace renaming, not namespace aliasing. – gnobal Feb 20 '11 at 12:31
  • @gnobal: This: `using namespace glm_quat = glm::gtc::quaternion;` doesn't work. I'm getting compiler errors. – Marnix Feb 20 '11 at 12:34
  • 1
    @Mamix: You need to declare the alias first (without using), and then use "using". and dont forget the ::quat after quaternion – MestreLion Feb 20 '11 at 12:41
  • it is namespace aliasing, take a look at [7.3.2] of the standard. You should be allowed to extend the glm namespace even if it is not your own as long as it is not std(I'm not 100% sure about this, but I couldn't find anything that forbids it in the standard in the short time) I never heard of namespace aliasing with a using directive but I don't know why you want this either(or what it should do). – DaVinci Feb 20 '11 at 12:42
2

You cant use the ":" character in the left side of a #DEFINE (the "name" part). But it is allowed in the right side (the "value")

So, try this:

#define GLM_QUAT glm::gtc::quaternion::quat

class Camera
{
protected:
    GLM_QUAT m_mRotation;
};

Remember that a #DEFINE is just a pre-compilation macro, in the "name value" pair form. The left part has the same rules as a variable name, and the right side can be anything. Its just a text that the compiler replaces in your code before doing any compiling / sintax checking.

By the way, i strongly suggest that you use UPPER_CAPS in your #DEFINE names, so you can easily spot them in your code, this avoids LOTS of confusion when debugging your code. You DONT want to misread it as a variable or type. Caps help to make it clear for you that it is a macro.

MestreLion
  • 12,698
  • 8
  • 66
  • 57
  • Didn't know that! Thanks. But now I see the namespace alias, I think that will do better for me and is more specific as an answer. Do you think it is possible to also do that in a `using namespace`? – Marnix Feb 20 '11 at 12:28
  • 1
    Yes, being a #DEFINE simply a text replace, you can use it everywhere, anywhere. The substition happens BEFORE any compiling is done. So the actual code processed would be the same as if you typed the whole "glm::gtc::quaternion::quat". Its just a text replacement the compiler do for you. – MestreLion Feb 20 '11 at 12:36