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

"Accumulation" of default template arguments across template declarations

27 views
Skip to first unread message

Andrey Tarasevich

unread,
May 3, 2021, 12:33:34 PM5/3/21
to
Hello

MSVC++ and Clang accept this code without any complaints

#include <iostream>

template <typename T, typename U> void foo();

void bar1()
{
foo<int, int>();
}

template <typename T, typename U = int> void foo();

void bar2()
{
foo<int>();
}

template <typename T = int, typename U> void foo()
{
std::cout << "Hello World" << std::endl;
}

int main()
{
bar1();
bar2();
foo();
}

GCC barks at 'foo<int>()' call

error: no matching function for call to 'foo<int>()'
14 | foo<int>();
| ^
note: candidate: 'template<class T, class U> void foo()'
3 | template <typename T, typename U> void foo();
| ^~~
note: template argument deduction/substitution failed:
note: couldn't deduce template parameter 'U'
14 | foo<int>();

One'd naturally expect default arguments for template parameters follow
the same general semantics as arguments for function parameters: they
accumulate across multiple sequential declarations.

But it is not the case in GCC. Who's right here? GCC? Or MSVC++ and Clang?

--
Best regards,
Andrey Tarasevich

Andrey Tarasevich

unread,
May 4, 2021, 1:01:22 AM5/4/21
to
On 5/3/2021 9:33 AM, Andrey Tarasevich wrote:
>
> But it is not the case in GCC. Who's right here? GCC? Or MSVC++ and Clang?
>

OK, I see that the standard is rather straightforward about it

"The set of default template-arguments available for use is obtained by
merging the default arguments from all prior declarations of the
template in the same way default function arguments are
([dcl.fct.default]).
[Example 6:
template<class T1, class T2 = int> class A;
template<class T1 = int, class T2> class A;
is equivalent to
template<class T1 = int, class T2 = int> class A;
— end example]"

http://eel.is/c++draft/temp.param#13

So, this is indeed a bug in GCC.

David Brown

unread,
May 4, 2021, 3:04:59 AM5/4/21
to
Don't forget to register it in the gcc bugzilla.


0 new messages