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

Explicit instantiation definitions for class templates generate double definitions #411

Closed
mariusbancila opened this issue Aug 9, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@mariusbancila
Copy link

Example:

namespace ns
{
   template <typename T>
   struct foo
   {
      T value;
   };

   template class foo<int>;        // [1]
}

template struct ns::foo<double>;   // [2]

int main()
{
}

Result is:

namespace ns
{
  template<typename T>
  struct foo
  {
    T value;
  };
  
  template<>
  struct foo<int>
  {
    int value;
  };
  
  template<>
  struct foo<double>
  {
    double value;
  };
  
  template<>
  struct foo<int>
  {
    int value;
  };
  
  
}



template<>
struct ns::foo<double>
{
  double value;
};


   // [2]

int main()
{
}

Live demo: https://t.co/puhOYbbT3c?amp=1

The same does not happen for function templates.
Example:

namespace ns
{
   template <typename T>
   void foo(T const value)
   {
   }

   template void foo(int);        // [1]
}

template void ns::foo(double);   // [2]

int main()
{
}

Result:

namespace ns
{
  template<typename T>
  void foo(const T value)
  {
  }
  
  /* First instantiated from: insights.cpp:8 */
  #ifdef INSIGHTS_USE_TEMPLATE
  template<>
  void foo<int>(const int value)
  {
  }
  #endif
  
  
  /* First instantiated from: insights.cpp:11 */
  #ifdef INSIGHTS_USE_TEMPLATE
  template<>
  void foo<double>(const double value)
  {
  }
  #endif
  
  
}

template void ns::foo(double);   // [2]

int main()
{
}

Live demo: https://t.co/OkuTBT3Lwd?amp=1

@andreasfertig
Copy link
Owner

Hello @mariusbancila,

thanks for spotting and reporting this issue. A fix is on its way.

Andreas

@andreasfertig andreasfertig added the bug Something isn't working label Aug 10, 2021
andreasfertig added a commit that referenced this issue Aug 10, 2021
Fixed #411: Wait for explicit instantiation in AST.
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

No branches or pull requests

2 participants