1

Currently programming a specialized Standard Library, and I find that in a particular case this is necessary for me:

namespace std
{
  // key, value
  template<class K, class V>
  using vector_map = other_namespace::vector_map<K, V>;

  // key, value, compare
  template<class K, class V, class C>
  using vector_map = other_namespace::vector_map<K, V, C>;
}

It does, however, not work. Not surprising. But what are my options to achieving this? I have thought about using the precprocessor, but I want to know what you guys think.

I want to be able to selective alias template class into another namespace, if possible.

Solution (in my case) was to add a default value instead of having several usings:

namespace std
{
  // key, value, compare
  template<class K, class V, class C = default_value>
  using vector_map = other_namespace::vector_map<K, V, C>;
}
gonzo
  • 442
  • 4
  • 15
  • 1
    What are you trying to achieve? `other_namespace::vector_map` isn't "overloaded" either, it just has a default for `C`. Could that work for you too? – Cameron Oct 08 '14 at 22:12
  • I want to have the same functionality as if the template class itself was in the same namespace that my using statement is in. In this example vector_map is in other_namespace, but I want to move specific variants over to std:: namespace: and types. – gonzo Oct 08 '14 at 22:15
  • Yes, thank you! I just added the default value to my template alias, and that solves it. Thanks again – gonzo Oct 08 '14 at 22:20
  • I don't get why you put this declaration into `namespace std`? You're about to produce "undefined behavior", see [here](http://stackoverflow.com/questions/16122912/is-it-ok-to-specialize-stdnumeric-limitst-for-user-defined-number-like-class) for example. – davidhigh Oct 08 '14 at 22:29
  • I know, and I'm still thinking about all of this. This is for an operating system without a timer interrupt, so there can be no (complete) standard library anyways. I am currently using `std` only because it's going to be familiar to users. Ultimately it's not my decision though. – gonzo Oct 08 '14 at 22:36
  • There is no `vector_map` in `std`, so why would having it in `std` make your users feel familiar? – Yakk - Adam Nevraumont Oct 09 '14 at 18:16

1 Answers1

6

If you want to write a fancy conditional forwarder, you'll have to not just use using.

template<class A, class B, class... C>
struct vector_map_helper {
  using type = other_namespace::vector_map<A,B>;
};
// specialize for 3:
template<class A, class B, class C>
struct vector_map_helper<A,B,C> {
  using type = other_namespace::vector_map<A,B,C>;
};
template<class A, class B, class C, class D, class...Z>
struct vector_map_helper<A,B,C,D, Z...>; // error 4 or more

template<class A, class B, class... C>
using vector_map = typename vector_map_helper<A,B,C...>::type;

in general, even if you are implementing a std library, you should avoid adding any "user facing" interfaces to your std that do not come from the std library. And the stuff you do support should match std specifications.

Have a nonstd or a std_ext namespace for non-std extensions. This will both make existing code either fail to compile or work when ported over, and it will avoid training your programmer-users in bad habits about what is in std.

It is also illegal to add most things to std with narrow exceptions, like std::hash specializations.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524