12

I have to use an API provided by a DLL with a header like this

namespace ALongNameToType {
    class ALongNameToType {
        static void Foo();   
    }
}

Is there a way to use ALongNameToType::ALongNameToType::Foo without having to type ALongNameToType::ALongNameToType each time? I tried using using namespace ALongNameToType but got ambiguous symbol errors in Visual Studio. Changing the namespace name or removing it gives me linker errors.

jww
  • 97,681
  • 90
  • 411
  • 885
Steven
  • 2,538
  • 3
  • 31
  • 40
  • Possible duplicate of [Classes and namespaces sharing the same name in C++](http://stackoverflow.com/questions/4070915/classes-and-namespaces-sharing-the-same-name-in-c) – jww May 05 '17 at 18:11

4 Answers4

24

I don't know what's ambiguous, but you can avoid all conflicts with other Foo functions like this:

namespace ALongNameToType {
    struct ALongNameToType {
        static void Foo();   
    };
}

typedef ALongNameToType::ALongNameToType Shortname;

int main() {
    Shortname::Foo();
}
Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • I forgot typedef totally... +1 – Khaled Alshaya Oct 08 '09 at 17:54
  • you dont actually have to use struct instead of class, do you?? – 2am Oct 13 '13 at 09:36
  • 2
    @2am: you don't have to, you could write `class ALongNameToType { public: static void Foo(); };` if you prefer, but the code in the question needs fixing one way or the other. I generally use `struct` for types with no non-public members. You should lay out the conditions for using `struct` in whatever style guide applies to the code. Some people use `struct` specifically for POD types, or specifically for types whose definition will compile as C, or specifically for types with no user-defined constructors or member functions, or just use it haphazardly for types they consider "simple" ;-) – Steve Jessop Oct 13 '13 at 09:37
3
namespace myns = ALongNameToType;

It seems that you can't alias a class scope like this:

// second ALongNameToType is a class
namespace myns = ALongNameToType::ALongNameToType;

Maybe you could alias the function it self:

namespace foo
{
 class foo
 {
 public:
  static void fun()
  {

  }
 };
}

int main()
{
 void (*myfunc)() = foo::foo::fun;

 myfunc();
}
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
3
using ALongNameToType::ALongNameToType::Foo;

if you just want to use it as Foo().

David Thornley
  • 56,304
  • 9
  • 91
  • 158
  • 3
    Unfortunately it doesn't work for me in Visual Studio 2008 `error C2885: 'ALongNameToType::ALongNameToType::Foo': not a valid using-declaration at non-class scope` – Steven Oct 08 '09 at 20:52
1

There are three ways to use using. One is for an entire namespace, one is for particular things in a namespace, and one is for a derived class saying it doesn't want to hide something declared/defined in a base class. You can use the second of those:

using ALongNameToType::ALongNameToType

Unfortunately this isn't working for you (due to the ambiguity of the namespace and the class having the same name). Combining this type of using with a previous answer should get rid of the ambiguity:

namespace alntt = ALongNameToType;
using alntt::ALongNameToType;

But once you've renamed the namespace, you really don't need the using statement (as long as you're comfortable writing the (shortened) namespace every time you use the class:

namespace alntt = ALongNameToType;
alntt::ALongNameToType a;
...
Max Lybbert
  • 19,717
  • 4
  • 46
  • 69
  • This gives me `error C2874: using-declaration causes a multiple declaration of 'ALongNameToType::ALongNameToType'` in Visual Studio 2008 – Steven Oct 08 '09 at 20:54
  • Oh well, it's useful in cases where there isn't ambiguity (eg., "using std::cout"). – Max Lybbert Oct 09 '09 at 06:42