Why are typename template parameters implicitly recognized as types?

Very often in C ++ class definitions, especially libraries, feature class, etc., you see code that looks like the following snippet:

template <typename Bar, typename Baz> class Foo { using bar_type = Bar; using baz_type = Baz; // ... etc. } 

And only with these lines can you later refer to Foo<A,B>::bar_type or Foo<C,D>:baz_type . I am wondering: why does the language standard not require the compiler to automatically detect types using the name template parameters, that is, allow the deletion of two used strings and recognize Foo<A,B>::Bar as A and Foo<C,D>::Baz like D

This should not even break the existing code, since in Foo the identifiers Bar and Baz are already accepted.

+5
source share
1 answer

Parameter names are not part of the declared object. This is true for both functions and templates. The following code declares only two separate objects:

 extern void f(int, char, bool); extern void f(int a, char b, bool c); extern void f(int x, char b, bool z); template <typename> struct X; template <typename A> struct X; template <typename T> struct X; 

Please note in particular that the following code works great:

 template <typename T> struct X { void f(); }; // X<T>::f not yet defined template <typename U> void X<U>::f() {} // now it defined 

All attempts to obtain an additional structure from parameter names must deal with this situation. One of the most popular queries in this area is function parameters; to date, there has not been a satisfactory proposal for such an extension.

To some extent, all such suggestions will require parameter names to be part of the declared object. For example, for functions that would raise the question of whether to distort parameter names and expose them to the linker.

+7
source

All Articles