What is the bug in the following project?
main.cpp
#include "template_specialization_conflict_test.hpp"
int main()
{
std::cout << utils::my_template_function(0.555);
std::cout<<utils::my_template_function<double>(0.555);
return 0;
}
template_specialization_conflict_test.hpp
#ifndef UTILS__UTILS__UTILS__UTILS
#define UTILS__UTILS__UTILS__UTILS
#include <iostream>
namespace utils
{
// A generic function
template <class T>
T my_template_function(T parameter)
{
std::cout << "function template";
std::cout << parameter;
return parameter;
}
// Template Specialization
// A function specialized for double data type
template <>
double my_template_function<double>(double parameter)
{
std::cout << "function specialization on double";
std::cout << parameter;
return parameter;
}
}
#endif
template_specialization_conflict_test.cpp
#include "template_specialization_conflict_test.hpp"
namespace utils
{
//empty
}
Error
>------ Rebuild All started: Project: template_specialization_conflict_test, Configuration: x64-Debug ------
[1/1] Cleaning all built files...
Cleaning... 2 files.
[1/3] Building CXX object CMakeFiles\template_specialization_conflict_test.dir\main.cpp.obj
[2/3] Building CXX object CMakeFiles\template_specialization_conflict_test.dir\template_specialization_conflict_test.cpp.obj
[3/3] Linking CXX executable template_specialization_conflict_test.exe
FAILED: template_specialization_conflict_test.exe
cmd.exe /C "cd . && "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\template_specialization_conflict_test.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\10.0.17763.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\10.0.17763.0\x64\mt.exe --manifests -- "C:\PROGRA~2\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64\link.exe" /nologo CMakeFiles\template_specialization_conflict_test.dir\template_specialization_conflict_test.cpp.obj CMakeFiles\template_specialization_conflict_test.dir\main.cpp.obj /out:template_specialization_conflict_test.exe /implib:template_specialization_conflict_test.lib /pdb:template_specialization_conflict_test.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK Pass 1: command "C:\PROGRA~2\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64\link.exe /nologo CMakeFiles\template_specialization_conflict_test.dir\template_specialization_conflict_test.cpp.obj CMakeFiles\template_specialization_conflict_test.dir\main.cpp.obj /out:template_specialization_conflict_test.exe /implib:template_specialization_conflict_test.lib /pdb:template_specialization_conflict_test.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\template_specialization_conflict_test.dir/intermediate.manifest CMakeFiles\template_specialization_conflict_test.dir/manifest.res" failed (exit code 1169) with the following output:
C:\Users\pc\source\repos\template_specialization_conflict_test\out\build\x64-Debug\main.cpp.obj : error LNK2005: "double __cdecl utils::my_template_function<double>(double)" (??$my_template_function@N@utils@@YANN@Z) already defined in template_specialization_conflict_test.cpp.obj
C:\Users\pc\source\repos\template_specialization_conflict_test\out\build\x64-Debug\template_specialization_conflict_test.exe : fatal error LNK1169: one or more multiply defined symbols found
ninja: build stopped: subcommand failed.
Rebuild All failed.
How can I fix this?
CodePudding user response:
How can I fix this?
You could solve this by adding/using the keyword inline
for the specialization so the specialization would look like:
//note the keyword inline in the below specialization
template <> inline
double my_template_function<double>(double parameter)
{
std::cout << "function specialization on double";
std::cout << parameter;
return parameter;
}
This works as can be seen here.
Second way to solve this would be to move your specialization into the source file instead of the header. So your template_specialization_conflict_test.cpp would look like: template_specialization_conflict_test.cpp
#include "template_specialization_conflict_test.hpp"
namespace utils
{
// Template Specialization
// A function specialized for double data type
template <>
double my_template_function<double>(double parameter)
{
std::cout << "function specialization on double";
std::cout << parameter;
return parameter;
}
}
The program works as can be seen here and here(with gcc and clang).