https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69906

            Bug ID: 69906
           Summary: Feature request: better error message when dependent
                    function template parsing fails
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: steve.lorimer at gmail dot com
  Target Milestone: ---

When a dependent type is specified without the `typename` keyword, the error
message generated alludes to this fact:

        main.cpp: In function ‘void bar(T&)’:
        main.cpp:13:5: error: need ‘typename’ before ‘T:: type’ because ‘T’ is
a dependent scope
             T::type i = 0;
             ^

However, if a dependent function template is specified without the `template`
keyword, the error message doesn't allude to the missing `template` keyword:

        main.cpp:10:15: error: expected primary-expression before ‘int’
             type.func<int>();
                       ^

The feature request is to extend this helpful `typename` keyword error message
to the case where the missing keyword is `template`.

Example:

        struct Foo
        {
            template<typename T>
            void func() {}
        };

        template<typename T>
        void bar(T& type)
        {
            type.func<int>();
        }

        int main()
        {
            Foo foo;
            bar(foo);
        }

Error:

        main.cpp:10:15: error: expected primary-expression before ‘int’
             type.func<int>();
                       ^
        main.cpp:10:15: error: expected ‘;’ before ‘int’

The fix is to use the `template` keyword:

        template<typename T>
        void bar(T& type)
        {
            type.template func<int>();
        }

Suggested error:

        main.cpp: In function ‘void bar(T&)’:
        main.cpp:13:10: error: need ‘template’ before ‘func’ because ‘T’ is a
dependent scope
             type.func<int>();
                  ^

Reply via email to