Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't namespaces be template parameters?

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?

like image 543
Alex Avatar asked Oct 16 '12 00:10

Alex


People also ask

Can you template a namespace?

The new entity that is proposed in this paper is called a namespace template. It is a template used to create one or more namespaces. A usage of that template would be a “namespace template instantiation” which is a namespace.

What can be a template parameter?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)

Can a template be a template parameter?

Templates can be template parameters. In this case, they are called template parameters. The container adaptors std::stack, std::queue, and std::priority_queue use per default a std::deque to hold their arguments, but you can use a different container.

Which parameter is allowed for non-type template?

Which parameter is legal for non-type template? Explanation: The following are legal for non-type template parameters:integral or enumeration type, Pointer to object or pointer to function, Reference to object or reference to function, Pointer to member.


1 Answers

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.

like image 108
Pete Becker Avatar answered Oct 04 '22 08:10

Pete Becker