28

I understand that namespaces cannot be template parameters. See the question, "template specialized on a namespace":

Given:

namespace A {
  class Foo;
  class Bar;
}

namespace B {
  class Foo;
  class Bar;
}

I want to template a class on the namespace A or B such that the following works:

template<name> class C {
  name::Foo* foo;
  name::Bar* bar;
};

I was wondering why this is the case. I understand that templates aren't structures, but is there a technical limitation to the compiler's design? Or is there some significant trade off for implementing this functionality?

8
  • 2
    Interesting. I've never needed this, but I guess I can sort of see why you might want it... maybe..... sometimes......... On the other hand, is this really how we use namespaces? Is it what they're designed for? No, I don't think that it is. Oct 16, 2012 at 0:39
  • 3
    This just falls under the category of "it isn't because it isn't". Oct 16, 2012 at 0:40
  • 2
    Might make a good proposal for C++1x. Oct 16, 2012 at 0:41
  • You are missing a couple of typename keywords in your C template. Oct 16, 2012 at 0:47
  • 1
    Anybody tried to make a standard proposal for it? Mar 30, 2019 at 10:12

2 Answers 2

28

Back when Bjarne Stroustrup first started talking about templates in C++ standards meetings he mentioned namespaces as template parameters. The reaction was skeptical, in part because namespaces themselves were so new, and we were afraid of combining two things that we didn't understand.

4
  • 18
    @windfinder - I was one of the skeptics. Oct 18, 2012 at 13:03
  • 4
    You aren't exactly a secondary source, are ya?! ;-)
    – Alex
    Oct 18, 2012 at 13:51
  • @PeteBecker any chance you remember around when that meeting was, or even better, have a link to some online conversation about it? I was having a discussion and wanted to link to the original source material if possible.
    – Catskul
    Sep 30, 2019 at 16:32
  • 2
    @Catskul -- sorry, I can't really help. The first meeting of X3J16 was in 1990, and the discussions of future directions would have been in the first few meetings. Sep 30, 2019 at 19:14
15

This would be:

  1. (IMO) Inappropriate: Namespaces avoid name clashes. Polymorphism is outside their charter.
  2. Unnecessary: It would achieve nothing that can't already be done with structs.
  3. Possibly difficult: A namespace isn't a complete, self-contained entity. Different members of a namespace can be declared in different headers and even different compilation units.
11
  • 10
    Polymorphism was outside of the charter of user-defined types until it was added to them. Oct 16, 2012 at 0:44
  • 4
    I think you clinched it with item 3, though. Seems like that alone would make implementation intractible, and semantics nonsensical. Oct 16, 2012 at 0:44
  • 1
    Adding to the unnecessary: you can always do namespace A { struct types { typedef Foo Foo; typedef Bar Bar; }; } and then use A::types as template argument. But I feel 1. is the most important reason (it was never designed for this use). Although 2. determines that there is little value to accept this as a new feature of the language. Oct 16, 2012 at 0:45
  • 1
    @LightnessRacesinOrbit: I agree that item 1 isn't a strong argument by itself, but it gains substantial weight when you factor in item 2. Oct 16, 2012 at 0:46
  • I can understand arguments for 1 & 2, but I've always felt that if something makes sense it should be allowed (permissive with confines of the precepts), so I feel 2 is pretty weak. 3 is the real interesting question. I feel like this is easily implemented at compilation time, but might be impossible to implement at link time?
    – Alex
    Oct 16, 2012 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.