October 21, 2024
Chicago 12, Melborne City, USA
templates

MSVC fails to specialize member of template class defined in a separate translation unit


I’ve been trying to compile VTK 9.3.1 with Visual Studio 17 2022, but it fails at the linking stage:

vtkCommonCore-9.3.lib(vtkSMPToolsAPI.obj) : error LNK2019: unresolved external symbol "public: bool __cdecl vtk::detail::smp::vtkSMPToolsImpl<1>::IsParallelScope(void)" (?IsParallelScope@?$vtkSMPToolsImpl@$00@smp@detail@vtk@@QEAA_NXZ) referenced in function "public: bool __cdecl vtk::detail::smp::vtkSMPToolsAPI::IsParallelScope(void)" (?IsParallelScope@vtkSMPToolsAPI@smp@detail@vtk@@QEAA_NXZ)

I found that the issue had already been reported, e.g. here, yet it remains unsolved.

I simplified the problem down to the following MRE:

Brick.h:

#ifndef Brick_h
#define Brick_h

#include <cstddef>


enum class ShapeType
{
    Circle   = 0,
    Triangle = 3,
    Square   = 4
};


template< ShapeType Shape >
class Brick
{
public:
    std::size_t GetNumberOfSides() 
    { 
        return static_cast< std::size_t >( Shape );
    }
};

#endif // Brick_h

Circle.cxx:

#include "Brick.h"


template<>
std::size_t Brick< ShapeType::Circle >::GetNumberOfSides()
{
    throw 0;
}

main.cxx:

#include <iostream>

#include "Brick.h"


template<>
std::size_t Brick< ShapeType::Circle >::GetNumberOfSides();

int main()
{
    Brick< ShapeType::Circle > B;
    
    std::cout << B.GetNumberOfSides();
    
    return 0;
}

With the standard set to C++17, MinGW compiles the code just fine while MSVC fails with the same type of linking error:

main.obj : error LNK2019: unresolved external symbol "public: unsigned __int64 __cdecl Brick<0>::GetNumberOfSides(void)" (?GetNumberOfSides@?$Brick@$0A@@@QEAA_KXZ) referenced in function main

It seems to me that for some reason the compiler fails to put the definition of Brick< ShapeType::Circle >::GetNumberOfSides into the object file Circle.obj, which is why the linker can not find it.

My question is: Is the above minimal example correct according to the C++17 standard?

In other words, is this a bug in the Microsoft compiler or is it MinGW (GCC) that is overly permissive?

Note: MSVC compiles the code correctly when set to the C++20 standard.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video