Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
languagelawyer opened this issue Jun 27, 2019 · 1 comment · Fixed by #204
Closed

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

languagelawyer opened this issue Jun 27, 2019 · 1 comment · Fixed by #204
Labels
bug Something isn't working

Comments

@languagelawyer
Copy link

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>
@andreasfertig andreasfertig added the bug Something isn't working label Jun 27, 2019
@andreasfertig
Copy link
Owner

Hello @languagelawyer,

thank you for the detailed analysis! This gave me a good glue where to look.

Andreas

andreasfertig added a commit that referenced this issue Jun 28, 2019
Fixed #200: A FunctionDecl template specialization was missing templa…
andreasfertig added a commit that referenced this issue Jun 29, 2019
With fix for #200 user-provided template specializations where guarded
by an `#if` which does modify the original code. This patch reverts that
behaviour and leaves the rest of the #200 fix as it is.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants