0

I know the C++ Concepts proposal is intended, perhaps among other things, to place restrictions on template parameters (say, being a "Sequence"), over the current situation in which whatever manages to compile is good enough (and the error messages are abysmal).

But - what about namespaces? I mean, currently, we can't use them as template parameters, but one would think that if a method only uses the static methods and members of a class, then a namespace should also be a satisfactory thing to pass to it. Does the current version of / current implements of the Concepts proposal support that? If not, was this considered and rejected or just not considered?

Related question:

Is a class with only static methods better than a namespace with only non-member functions?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 6
    You cannot pass namespaces. – Yakk - Adam Nevraumont Aug 25 '17 at 20:14
  • @Yakk: I don't want to pass them, but maybe I can use them as template parameters. – einpoklum Aug 25 '17 at 20:17
  • 6
    You cannot do that. So, what is your question? – Yakk - Adam Nevraumont Aug 25 '17 at 20:23
  • @Yakk: I can't do it _now_. But maybe I would be able to do it with Concepts. That would "save" me having to make classes of certain namespaces. (Although, granted, I suppose you could argue classes with no non-static members are valid constructs.) – einpoklum Aug 25 '17 at 20:24
  • The answer is no. Neither the C++20 working draft nor the old Concepts TS contain anything remotely like that. You could write a proposal for a future language revision, though. – Kerrek SB Aug 25 '17 at 20:37
  • 2
    A `namespace` is a mechanism to provide names (of classes, functions, variables, enums, etc) to prevent conflicts. I doubt the `namespace` abstraction will ever morph to the point where it can satisfy a `Concept`. – R Sahu Aug 25 '17 at 20:39
  • @RSahu: And what's the difference between that and a final class with only static members? Also, a namespace is also used to group code together when there's no "instantiation" involved like with classes. – einpoklum Aug 25 '17 at 20:56
  • @einpoklum, That does degenerate to something analogous to a namespace, doesn't it? However, I have to ask the question. Why you would you pollute the namespace abstraction if a class, degenerate or not, can give you what you need? – R Sahu Aug 25 '17 at 21:16
  • Re, "a namespace is also used to group code together..." How so? If I declare variables `x`, `y`, and `z` inside `namespace foo {...}`, those variables do not belong to the namespace. Only their _names_ belong to the namespace. Namespaces are just a trick to save me from having to type `Foo_x`, and `Foo_y`, and `Foo_z` everywhere. – Solomon Slow Aug 25 '17 at 21:20
  • @jameslarge: "those variables do not belong the namespace." Sure they do. I "access" them through the namespace (I mean, syntactically). "Only their *names* belong to the namespace" - when I write code, I use their names. I could use their addresses, but that's a separate matters. And - your assumption that namespace are only used the way you think they should be, or for what the original motivation for introducing them was, is baseless. Just thing of how TMP evolved. – einpoklum Aug 25 '17 at 21:50
  • @RSahu: "Why would you pollute the namespace abstraction if a class... can give you what you want?" Really, it's the opposite. Why would I pollute the class abstraction for something which is not supposed to (and in fact, cannot) have instances? – einpoklum Aug 25 '17 at 21:54
  • @einpoklum a namespace *is* a singleton class anyway. – Massa Nov 15 '17 at 12:39

1 Answers1

2

Concepts adds no mechanism to pass namespaces at compile or run time. So there is no way to test a namesoace against a conceot, or parametarize code with a namespace, barring macros.

The reflection TS may permit reflection over namespaces (I am not up to date on its current status), but that is orthogonal to concepts. Maybe reification and reflection of namespaces can be manipulated to permit concept checking of namespaces and passimg them around somehow, but if it does today it might not tomorrow and vice vers as it relies on two different plastic features where such a side effect would be accidental at best.

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