c++에서 템플릿 함수간 템플릿 타입 passing
글쓴이: zkdnsxjaos / 작성시간: 목, 2019/12/12 - 5:47오후
아래와 같이 tell.h는 tell함수와 그 구현체의 함수인 tell_impl 들의 signiture를 가지고 있습니다.
두 함수 모두 BOOL_TYPE 이라는 템플릿 타입을 활용합니다.
tell.h
template <typename BOOL_TYPE> void tell_impl(); template <typename BOOL_TYPE> void tell();
tell.cpp
#include <iostream> template <typename BOOL_TYPE> void tell_impl() { if(BOOL_TYPE()) std::cout << "TRUE\n"; else std::cout << "FALSE\n"; } template <typename BOOL_TYPE> void tell() { tell_impl<BOOL_TYPE>(); }
위의 헤더와 구현체를 통해 메인함수에서는 아래와 같이 tell 함수를 호출합니다.
main.cpp
#include <type_traits> #include "tell.h" tell<std::true_type>(); // expected to print "TRUE" tell<std::false_type>(); // expected to print "FALSE"
그러나 아래와 같은 undefined reference 에러가 컴피일시 발생합니다.
====================[ Build | test | Debug ]================================ /.../tools/cmake-3.14.1-Linux-x86_64/bin/cmake --build /.../test/cmake-build-debug --target test -- -j 1 Scanning dependencies of target test [ 33%] Building CXX object CMakeFiles/test.dir/main.cpp.o [ 66%] Linking CXX executable test CMakeFiles/test.dir/main.cpp.o: In function `main': /.../main.cpp:5: undefined reference to `void tell<std::integral_constant<bool, true> >()' /.../main.cpp:6: undefined reference to `void tell<std::integral_constant<bool, false> >()'
여기서 std::true_type / std::false_type을 템플릿 함수간에 passing 하기 위해 무엇이 수정되어야 할까요?
Forums:
여기 나와 있네요
https://stackoverflow.com/questions/1724036/splitting-templated-c-classes-into-hpp-cpp-files-is-it-possible
여러 가지 방법이 있지만, .cpp를 .h로 변경하고 그걸 인클루드 하는 것을 흔히 권하는 것 같습니다. 이유는 코멘트 여기저기에 잘 나와 있으니 읽어보세요.
고맙습니다
좋은하루 되세요!
댓글 달기