Featured post
c++ - "multiple definition of..." error for a full specialisation of a template function -
have project configuration:
./main.cpp ./type_traints/typetraints.cpp ./type_traints/typetraints.hpp ./type_traints/chapter_20.hpp
the ./type_traints/cmakelists.txt file is:
cmake_minimum_required (version 2.8) add_library(chapter_20 typetraints.cpp)
and ./cmakelists.txt follows:
cmake_minimum_required (version 2.8) project (mpl) add_subdirectory(type_traints) include_directories(type_traints) link_directories(type_traints) add_executable (mpl main.cpp) target_link_libraries(mpl chapter_20)
relevant parts of files (most includes omitted) include:
./type_traints/chapter_20.hpp
#ifndef chapter_20_guard #define chapter_20_guard #include <typetraints.hpp> void chapter_20() { test_23(); } #endif //chapter_20_guard
./type_traints/typetraints.hpp
#ifndef type_traints_guard #define type_traints_guard namespace details { template<class t> const char* class2name() { return "unknown"; }; template<> const char* class2name<int>() { return "int"; }; } template<class t> class type_descriptor { friend std::ostream& operator << (std::ostream& stream, const type_descriptor<t>& desc) { stream << desc.getname(); return stream; } public: std::string getname() const; }; template<class t> std::string type_descriptor<t>::getname() const { return details::class2name<t>(); } void test_23(); #endif // type_traints_guard
./type_traints/typetraints.cpp
#include<typetraints.hpp> void test_23() { cout << type_descriptor<int>() << endl; }
and ./main.cpp
#include <chapter_20.hpp> int main(int argc, char* argv[]) { chapter_20(); return 0; }
the project compiles fails link:
[ 50%] building cxx object type_traints/cmakefiles/chapter_20.dir/typetraints.cpp.o linking cxx static library libchapter_20.a [ 50%] built target chapter_20 [100%] building cxx object cmakefiles/mpl.dir/main.cpp.o linking cxx executable mpl type_traints/libchapter_20.a(typetraints.cpp.o): in function `char const* details::cl ass2name<int>()': /home/marcin/projects/mpl/type_traints/typetraints.hpp:312: multiple definition of `c har const* details::class2name<int>()' cmakefiles/mpl.dir/main.cpp.o:/home/marcin/projects/mpl/type_traints/typetraints.hpp: 312: first defined here collect2: ld returned 1 exit status make[2]: *** [mpl] błąd 1 make[1]: *** [cmakefiles/mpl.dir/all] error 2 make: *** [all] error 2 23:56:20@marcin-laptop ~/p
the project links fine if remove class2name specialization (class2name<int>()
) typetraints.hpp , use generic implementation.
does have idea why that? did miss-configure cmake files?
in short: explicitly (i.e. fully) specialized template function is no longer template. ordinary function , obeys 1 definition rule ordinary functions.
in other words can't define explicitly specialized function templates in header file. doing result in odr violations.
a template template long depends on @ least 1 parameter. i.e. partial specializations can defined in header files (since still templates). explicit (i.e. full) specializations can declared in header files, have defined in implementation files, ordinary functions.
- Get link
- X
- Other Apps
Comments
Post a Comment