Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

explicit specialization problem

70 views
Skip to first unread message

Bonita Montero

unread,
Jan 7, 2020, 5:26:31 AM1/7/20
to
Why does this work with an older g++ (6.3.0) and not with MSVC:

#include <iostream>

using namespace std;

template<typename T>
struct My
{
};

template<typename T>
struct Lib
{
void f();
};

template<typename T>
void Lib<T>::f()
{
cout << "void Lib<T>::f()" << endl;
}

template<>
template<typename T>
struct Lib<My<T>>
{
void f();
};

template<typename T>
void Lib<My<T>>::f()
{
cout << "void Lib<My<T>>::f()" << endl;
}

int main()
{
Lib<My<int>> lmi;
lmi.f();
}

MSVC:
x.cpp(22): error C2910: 'Lib<My<T>>': cannot be explicitly specialized
x.cpp(31): error C2039: 'f': is not a member of 'Lib<My<T>>'
x.cpp(30): note: see declaration of 'Lib<My<T>>'
x.cpp(31): error C2447: '{': missing function header (old-style formal
list?)

Ian Collins

unread,
Jan 7, 2020, 4:26:25 PM1/7/20
to
Use a more friendly compiler...

$ clang++ -std=c++17 /tmp/x.cc
/tmp/x.cc:22:1: warning: extraneous template parameter list in template
specialization
template<>
^~~~~~~~~~

--
Ian.


Alf P. Steinbach

unread,
Jan 7, 2020, 5:23:21 PM1/7/20
to
On 07.01.2020 11:26, Bonita Montero wrote:
> Why does this work with an older g++ (6.3.0) and not with MSVC:
>
> #include <iostream>
>
> using namespace std;
>
> template<typename T>
> struct My
> {
> };
>
> template<typename T>
> struct Lib
> {
>     void f();
> };
>
> template<typename T>
> void Lib<T>::f()
> {
>     cout << "void Lib<T>::f()" << endl;
> }
>
> template<>
> template<typename T>
> struct Lib<My<T>>
> {
>     void f();
> };

The `template<>` does not make sense here.

Just remove that line.


> template<typename T>
> void Lib<My<T>>::f()
> {
>     cout << "void Lib<My<T>>::f()" << endl;
> }
>
> int main()
> {
>     Lib<My<int>> lmi;
>     lmi.f();
> }
>
> MSVC:
> x.cpp(22): error C2910: 'Lib<My<T>>': cannot be explicitly specialized
> x.cpp(31): error C2039: 'f': is not a member of 'Lib<My<T>>'
> x.cpp(30): note: see declaration of 'Lib<My<T>>'
> x.cpp(31): error C2447: '{': missing function header (old-style formal
> list?)

I get the same diagnostics with MSVC 2019.

With MinGW g++ 9.2:

_.cpp:25:8: error: too many template-parameter-lists
25 | struct Lib<My<T>>
| ^~~~~~~~~~
_.cpp:31:20: error: invalid use of incomplete type 'struct Lib<My<T> >'
31 | void Lib<My<T>>::f()
| ^
_.cpp:12:8: note: declaration of 'struct Lib<My<T> >'
12 | struct Lib
| ^~~

When the offending `template<>` is removed both compilers accept the code.


- Alf



Bonita Montero

unread,
Jan 8, 2020, 1:29:25 AM1/8/20
to
>> template<>
>> template<typename T>
>> struct Lib<My<T>>
>> {
>>      void f();
>> };

> The `template<>` does not make sense here.
> Just remove that line.

Thanks.

Chris M. Thomasson

unread,
Jan 8, 2020, 1:34:59 AM1/8/20
to
Your code works for me wrt removing the extra 'template<>'. Actually, in
gcc in default, it works as is. I get an output of:

Bonita Montero

unread,
Jan 8, 2020, 1:46:55 AM1/8/20
to
> Your code works for me wrt removing the extra 'template<>'.
> Actually, in gcc in default, it works as is. I get an output of:
> void Lib<My<T>>::f()

I think the template<> makes sense because in this case I have also
a template<>:

template<typename T>
struct S
{
};

template<>
struct S<int>
{
};

When it would be up to Alf I would have to write only:

struct S<int>
{
};

... which doesn't work.

Öö Tiib

unread,
Jan 8, 2020, 1:59:03 AM1/8/20
to
You had:

template<>
template<typename T>




Bonita Montero

unread,
Jan 8, 2020, 4:36:49 AM1/8/20
to
> You had:
> template<>
> template<typename T>

Yes, but makes also sense and it is accepted by g++.

Öö Tiib

unread,
Jan 8, 2020, 6:56:59 AM1/8/20
to
It does not make sense in that context but lot of code that does not
make sense and even violates some rules is (often incorrectly)
accepted without any diagnostic by one or other compiler.

Bonita Montero

unread,
Jan 8, 2020, 7:00:36 AM1/8/20
to
>>> You had:
>>> template<>
>>> template<typename T>
>> Yes, but makes also sense and it is accepted by g++.

> It does not make sense in that context ...

And why does g++ accept this ?

Öö Tiib

unread,
Jan 8, 2020, 9:02:24 AM1/8/20
to
On Wednesday, 8 January 2020 14:00:36 UTC+2, Bonita Montero wrote:
> >>> You had:
> >>> template<>
> >>> template<typename T>
> >> Yes, but makes also sense and it is accepted by g++.
>
> > It does not make sense in that context ...

Restoiring:
"but lot of code that does not make sense and even violates some
rules is (often incorrectly) accepted without any diagnostic by
one or other compiler."

>
> And why does g++ accept this ?

Because the part that you snipped that I restored above. Can't
you read more than 6 words?

Bonita Montero

unread,
Jan 8, 2020, 9:15:20 AM1/8/20
to
> Restoiring:
> "but lot of code that does not make sense and even violates some
> rules is (often incorrectly) accepted without any diagnostic by
> one or other compiler."

If you have a template-specialization of a class that implements
all template-parameters it is headed with "template<>".

Öö Tiib

unread,
Jan 8, 2020, 9:48:44 AM1/8/20
to
But not with "template<> template<typename T>"

Bonita Montero

unread,
Jan 8, 2020, 9:53:17 AM1/8/20
to
Am 08.01.2020 um 15:48 schrieb Öö Tiib:
>> If you have a template-specialization of a class that implements
>> all template-parameters it is headed with "template<>".

> But not with "template<> template<typename T>"

No, that's not related to the specialization-part.

Ben Bacarisse

unread,
Jan 8, 2020, 10:00:45 AM1/8/20
to
It's not accepted by recent gcc versions (8.x and above).

--
Ben.

Öö Tiib

unread,
Jan 8, 2020, 10:17:00 AM1/8/20
to
Fixed g++ also complains about too large count of template parameter lists:
http://coliru.stacked-crooked.com/a/417e954f5e129b64
So whatever you tried to tell with that code does not make sense to it
as well.

Manfred

unread,
Jan 8, 2020, 10:28:34 AM1/8/20
to
After restoring proper (rudely removed) attributions,

Alf P. Steinbach wrote:
> Bonita Montero wrote:
>> template<>
>> template<typename T>
>> struct Lib<My<T>>
>> {
>> void f();
>> };
>
> The `template<>` does not make sense here.

It is clearly related to specialization. Ref:

template<typename T>
struct S
{
};

template<typename T>
struct S<T*>
{
};

Chris M. Thomasson

unread,
Jan 8, 2020, 5:45:04 PM1/8/20
to
Fwiw, still working with:

g++.exe (Rev1, Built by MSYS2 project) 7.3.0


In Clang 7.0.0-3 I get:

main.cpp:22:1: warning: extraneous template parameter list in template
specialization

Ben Bacarisse

unread,
Jan 8, 2020, 9:20:18 PM1/8/20
to
"Chris M. Thomasson" <chris.m.t...@gmail.com> writes:

> On 1/8/2020 7:00 AM, Ben Bacarisse wrote:
>> Bonita Montero <Bonita....@gmail.com> writes:
>>
>>>>>> You had:
>>>>>> template<>
>>>>>> template<typename T>
>>>>> Yes, but makes also sense and it is accepted by g++.
>>>
>>>> It does not make sense in that context ...
>>>
>>> And why does g++ accept this ?
>>
>> It's not accepted by recent gcc versions (8.x and above).
>>
>
> Fwiw, still working with:
>
> g++.exe (Rev1, Built by MSYS2 project) 7.3.0

Not sure what you mean by "working". I just tried gcc.Godbolt.org and
the oldest version that rejects the code is 8.1.

--
Ben.

Chris M. Thomasson

unread,
Jan 8, 2020, 10:42:42 PM1/8/20
to
On 1/8/2020 6:20 PM, Ben Bacarisse wrote:
> "Chris M. Thomasson" <chris.m.t...@gmail.com> writes:
>
>> On 1/8/2020 7:00 AM, Ben Bacarisse wrote:
>>> Bonita Montero <Bonita....@gmail.com> writes:
>>>
>>>>>>> You had:
>>>>>>> template<>
>>>>>>> template<typename T>
>>>>>> Yes, but makes also sense and it is accepted by g++.
>>>>
>>>>> It does not make sense in that context ...
>>>>
>>>> And why does g++ accept this ?
>>>
>>> It's not accepted by recent gcc versions (8.x and above).
>>>
>>
>> Fwiw, still working with:
>>
>> g++.exe (Rev1, Built by MSYS2 project) 7.3.0
>
> Not sure what you mean by "working".

Compiling, and generating an executable.

Ben Bacarisse

unread,
Jan 9, 2020, 1:17:49 PM1/9/20
to
"Chris M. Thomasson" <chris.m.t...@gmail.com> writes:

> On 1/8/2020 6:20 PM, Ben Bacarisse wrote:
>> "Chris M. Thomasson" <chris.m.t...@gmail.com> writes:
>>
>>> On 1/8/2020 7:00 AM, Ben Bacarisse wrote:
>>>> Bonita Montero <Bonita....@gmail.com> writes:
>>>>
>>>>>>>> You had:
>>>>>>>> template<>
>>>>>>>> template<typename T>
>>>>>>> Yes, but makes also sense and it is accepted by g++.
>>>>>
>>>>>> It does not make sense in that context ...
>>>>>
>>>>> And why does g++ accept this ?
>>>>
>>>> It's not accepted by recent gcc versions (8.x and above).
>>>>
>>>
>>> Fwiw, still working with:
>>>
>>> g++.exe (Rev1, Built by MSYS2 project) 7.3.0
>>
>> Not sure what you mean by "working".
>
> Compiling, and generating an executable.

I see. That's what I said, though I gave more a general answer. Your
word "still" suggested you weren't simply give an example of what I'd
said.

--
Ben.

Jorgen Grahn

unread,
Jan 9, 2020, 2:40:26 PM1/9/20
to
I think what really happened was, when you wrote "not accepted by
recent gcc versions (8.x and above)" you had an implicit "but accepted
in 7.x" and Chris thought you implied "... and I don't know when it
stopped being accepted".

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Ben Bacarisse

unread,
Jan 9, 2020, 4:21:50 PM1/9/20
to
Oh yes. That's likely how it was read. Thanks. Anyway, not
disagreement exists about the facts!

--
Ben.
0 new messages