Skip to content

cppinsights loses template<> in explicit function template specializations #200

@languagelawyer

Description

@languagelawyer

For the following code

template<typename U, typename ...T>
void f(U, T... rest)
{
  if constexpr (sizeof...(rest) != 0)
    f(rest...);
}

template<>
void f<int>(int) {}

int main()
{
    f(0, 1);
}

cppinsights produces

template<typename U, typename ...T>
void f(U, T... rest)
{
  if constexpr (sizeof...(rest) != 0)
    f(rest...);
}

/* First instantiated from: insights.cpp:13 */
#ifdef INSIGHTS_USE_TEMPLATE
template<>
void f<int, int>(int, int __rest1)
{
  if constexpr(1 != 0) f(__rest1);
  
  
}
#endif


void f<int, >(int)
{
}


int main()
{
  f(0, 1);
}

Note that there is no template<> before the explicit specialization

void f<int, >(int)
{
}

Maybe because explicit specializations look similar to ordinary function declarations in the AST:

TranslationUnitDecl
|-FunctionTemplateDecl <line:1:1, line:6:1> line:2:6 f
...
| `-FunctionDecl <line:2:1, line:6:1> line:2:6 used f 'void (int, int)'
|   |-TemplateArgument type 'int'
|   |-TemplateArgument pack
|   | `-TemplateArgument type 'int'
...
|-FunctionDecl prev 0x5611456d1ff8 <line:8:1, line:9:19> col:6 used f 'void (int)'
| |-TemplateArgument type 'int'
| |-TemplateArgument pack
| |-ParmVarDecl <col:13> col:16 'int'
| `-CompoundStmt <col:18, col:19>

The f<int, int> instantiation is a child of the FunctionTemplateDecl, but the explicit specialization is not. It is a direct child of the TranslationUnitDecl.

However, an ordinary function declaration would not have the prev 0xXXX... part and TemplateArguments. void f(int) {} is turned into

|-FunctionDecl <line:11:1, col:14> col:6 f 'void (int)'
| |-ParmVarDecl <col:8> col:11 'int'
| `-CompoundStmt <col:13, col:14>

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions