Understanding C++ member function template specialization

前端 未结 2 851
后悔当初
后悔当初 2021-02-05 15:07

I have the following class:

#pragma once
#include 
#include 

class testclass
{
public:
    template  T item(const s         


        
2条回答
  •  粉色の甜心
    2021-02-05 15:43

    The problem boils down to the common problem of not having the templates in the header file. The compiler, when processing main does not see the specialization and it generates its own instantiation of the generic template for std::string. This is a violation of the ODR, as there are now 2 different specializations for std::string in the same program, but the compiler is not required to diagnose it.

    The simple solution is to declare/define the specialization in the header so that the compiler can either use it, or at least will know not to generate the specialization from the generic version when processing main

提交回复
热议问题