Can a parameter of a template template parameter cause shadowing?
Asked Answered
C

2

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.

Clyburn answered 2/3, 2016 at 10:18 Comment(6)
why do you need to use T twice? if you want to use T in the second template you can write template <class T, template <class S=T> class>Penthea
@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 T, template <class S> class> ?Clyburn
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?Clyburn
Can be this question related?Perdita
@DavidHaim OK, with template <template T, template <class S=T> class C>, we can replace C<T> with C<> as if C was defined with a default template parameter... This can be handy in some cases, but I'm not sure I'll be using it often. Anyways thanks so much.Clyburn
@Perdita 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.Clyburn
C
7

No. [temp.local]/6:

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

Chiefly answered 2/3, 2016 at 10:28 Comment(1)
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."Clyburn
S
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...
}
Sinistrad answered 30/1, 2021 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.