Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should `foo.template bar()` do when there's both a template and a non-template overload?

A coworker shared this code with me:

run on gcc.godbolt.org

#include <iostream>

struct A
{
    void foo() {std::cout << "1\n";}
    
    template <typename T = int>
    void foo() {std::cout << "2\n";}
};

int main()
{
    A x;
    x.template foo();
}

GCC prints 1, Clang prints 2, and MSVC complains about missing template arguments.

Which compiler is correct?

like image 569
HolyBlackCat Avatar asked Sep 05 '25 03:09

HolyBlackCat


1 Answers

[temp.names]/5 says that a name prefixed by template must be a template-id, meaning that it must have a template argument list. (Or it can refer to a class/alias template without template argument list, but this is deprecated in the current draft as a result of P1787R6 authored by @DavisHerring.)

There is even an example almost identical to yours under it, identifying your use of template as ill-formed.

The requirement and example comes from CWG defect report 96, in which the possible ambiguity without the requirement is considered.

Open GCC bug report for this is here. I was not able to find a Clang bug report, but searching for it isn't that easy. Its implementation status page for defect reports however does list the defect report as unimplemented.

like image 138
user17732522 Avatar answered Sep 08 '25 00:09

user17732522



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!