Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

c++ - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...