8

Is this legal C++?

template <typename T, template <typename T> class>
struct S { };

Clang (3.7.1) rejects it, complaining the second T shadows the first T. GCC seems not to care about it and I think that's reasonable. I think it is only the number of parameters that matters in a template template parameter.

cigien
  • 57,834
  • 11
  • 73
  • 112
nodakai
  • 7,773
  • 3
  • 30
  • 60
  • why do you need to use `T` twice? if you want to use `T` in the second template you can write `template class>` – David Haim Mar 02 '16 at 10:24
  • 1
    @DavidHaim It's not mandatory, but when the first parameter is supposed to be given to the second parameter which is a template template, it makes sense to use the same (or, at least a similar) name for the nested template parameter. Your suggestion is interesting, I've never tried to specify a default parameter to a template template parameter. What does it accomplish compared to `template class>` ? – nodakai Mar 02 '16 at 10:42
  • I wonder what is the driving motivation for people who does search&destroy against tag words in a post title?? Are they perhaps equally enthusiastic in replacing `NULL` to `nullptr`? – nodakai Mar 02 '16 at 10:48
  • Can be [this question](http://stackoverflow.com/questions/28992265/is-there-any-use-for-named-parameters-into-template-template-parameters) related? – PaperBirdMaster Mar 02 '16 at 11:15
  • @DavidHaim OK, with `template – nodakai Mar 02 '16 at 11:33
  • @PaperBirdMaster Thanks, they discuss when a parameter name of a template template parameter matters, and I was wrong in saying only the number of parameters would matter. – nodakai Mar 02 '16 at 11:57

2 Answers2

7

No. [temp.local]/6:

A template-parameter shall not be redeclared within its scope (including nested scopes).

T.C.
  • 133,968
  • 17
  • 288
  • 421
  • Hmm, it took me a while to realize your quote is actually relevant to my case, but right, it is surely about a "nested scope." – nodakai Mar 02 '16 at 10:46
0

While the right answer exists, it toke some time for myself to understand, and I just wanted to add an example:

template <class Key, class T>
class MyData {
public:
    // ...

    template <class Key, class T>
    inline static MyData<Key, T> *get(MyMap<Key, T> *ptr)
    {
        return NULL: // Logic here...
    }

    // ...
}

As "Template-parameters shall not be re-declared within its scope (including nested scopes)", above get(..) method should be changed and use other names, like:

template <class KeyType, class Type>
inline static MyData<KeyType, Type> *get(MyMap<KeyType, Type> *ptr)
{
    return NULL: // Logic here...
}
Top-Master
  • 7,611
  • 5
  • 39
  • 71