Skip to content

Explicit instantiation definitions for class templates generate double definitions #411

@mariusbancila

Description

@mariusbancila

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

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