admin管理员组

文章数量:1208155

I'm stuck defining a specialization for a member function inside a class template. If I define the fully specialized member function outside of the class, and then use it in my main, the linker complains of multiple definitions:

in template.hpp:

template <typename T>
class MyClassTemplate
{
public:
    MyClassTemplate(const T &t) : member(t)
    {
        
    }

    const T GetMember() const
    {
        return member;
    }

private:
    T member;
};

in templates.cpp:

template <>
const double MyClassTemplate<double>::GetMember() const
{
    return member * 2.0;
}

in main.cpp:

 MyClassTemplate<double> mctd(10.9);

 mctd.GetMember();

the error says that the linker found two definition of the same function, but I'm not defining the function, I'm using it!

I'm stuck defining a specialization for a member function inside a class template. If I define the fully specialized member function outside of the class, and then use it in my main, the linker complains of multiple definitions:

in template.hpp:

template <typename T>
class MyClassTemplate
{
public:
    MyClassTemplate(const T &t) : member(t)
    {
        
    }

    const T GetMember() const
    {
        return member;
    }

private:
    T member;
};

in templates.cpp:

template <>
const double MyClassTemplate<double>::GetMember() const
{
    return member * 2.0;
}

in main.cpp:

 MyClassTemplate<double> mctd(10.9);

 mctd.GetMember();

the error says that the linker found two definition of the same function, but I'm not defining the function, I'm using it!

Share Improve this question asked Jan 19 at 16:01 LucaLuca 1,7566 gold badges22 silver badges45 bronze badges 8
  • 1 Please post the complete and verbatim error message. – 3CxEZiVlQ Commented Jan 19 at 16:09
  • but I'm not defining the function What is that in the class template then? – 3CxEZiVlQ Commented Jan 19 at 16:11
  • 3 I see your specialization is in a cpp file. Typically template code only works when its all in a header file: stackoverflow.com/questions/495021/… – NathanOliver Commented Jan 19 at 16:11
  • 1 Unrelated: Why are you making the return value const? – Ted Lyngmo Commented Jan 19 at 16:34
  • Please a minimal reproducible example is required. Nothing can be resolved from partial code. – john Commented Jan 19 at 16:40
 |  Show 3 more comments

1 Answer 1

Reset to default 0

(As per comments by Igor Tandetnik...)

We might want to read the C++ Standard, Section 13.9.4 (Explicit specialization), Paragraph 7.

In the main.cpp translation unit, we are causing an implicit instantiation of MyClassTemplate<double>::GetMember() const without the explicit specialization's declaration's being reachable.

What happens in this case is that:

  • for the main.cpp translation unit, the MyClassTemplate<double>::GetMember() const member function is implicitly instantiated (from the code in template.hpp);

  • for the templates.cpp translation unit, the explicit specialization is instantiated;

  • the linker sees the different instantiations, and in the author's case it has signaled an error, but in my case (I have tried with Visual C++ 2019, g++-10, clang++-11) it does not signal an error ("no diagnostic is required" as well remembered by Igor Tandetnik in the comments a few days ago) and instead calls either the implicit instantiation or the explicit specialization (in my case: apparently depending on whether optimizations are enabled).

For the record, the code I have tried is:

// main.cpp:

#include "template.hpp"

#include <iostream>

int main ()
{
    MyClassTemplate<double> mctd(10.9);
    std::cout << mctd.GetMember() << "\n";
}


// template.hpp:

#pragma once

template <typename T>
class MyClassTemplate
{
public:
    MyClassTemplate(const T &t) : member(t)
    {

    }

    const T GetMember() const
    {
        return member;
    }

private:
    T member;
};


// templates.cpp:

#include "template.hpp"

template <>
const double MyClassTemplate<double>::GetMember() const
{
    return member * 2.0;
}

and the commands I have used for building are (in Linux):

(CXX='g++'; CXXFLAGS='-std=gnu++17 -O0 -g'; "${CXX}" ${CXXFLAGS} -c main.cpp -o main.o && "${CXX}" ${CXXFLAGS} -c templates.cpp -o templates.o && objdump --disassembler-options=intel --disassemble main.o templates.o && "${CXX}" ${CXXFLAGS} main.o templates.o -o main && ./main; echo $?)

and we can change -O0 to -O2 to see how the displayed result (the last line) changes (from 21.8 to 10.9, with CXX set to either g++-10 or clang++-11 in my case).

To fix the problem, in main.cpp, before the code which causes the implicit instantiation, we should have a declaration of the explicit specialization, i.e. we might add the following lines at the end of template.hpp:

template <>
const double MyClassTemplate<double>::GetMember() const;

and now the declaration of the explicit specialization is going to be reachable from any use which would cause an implicit instantiation...

... and we are consistently going to get the desired answer (21.8), with either toolchain and either optimization settings.

本文标签: cclass template member specialization multiple definitionsStack Overflow