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 | Show 3 more comments1 Answer
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, theMyClassTemplate<double>::GetMember() const
member function is implicitly instantiated (from the code intemplate.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
版权声明:本文标题:c++ - class template member specialization multiple definitions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738732426a2109399.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
const
? – Ted Lyngmo Commented Jan 19 at 16:34