Invalid use of incomplete type for partial template specialization c++ -
i trying specialize class method foo()
. works full template specialization. however, not work partial template specialization.
here example code compiles fine on gcc , clang :
#include <iostream> #include <string> template <typename key, typename value> struct simplekey { key key; value value; void foo() const { std::cout << "base" << std::endl; } }; /* // uncomment , won't work ! template<typename key> void simplekey<key, std::string>::foo() const { std::cout << "partial" << std::endl; } */ template<> void simplekey<int, std::string>::foo() const { std::cout << "full" << std::endl; } int main() { simplekey<double, std::string> key1{1.0,"key1"}; key1.foo(); simplekey<int, std::string> key2{1,"key2"}; key2.foo(); }
the error on clang , gcc when uncommenting relevant code :
error: invalid use of incomplete type ‘struct simplekey >’ void simplekey::foo() const {
what should partial template specialization work "minimal" efforts ?
you can explicitly specialize member function of particular implicit instantiation of class template. not allowed partial specializations. if don't want write full partial specialization, can consider using tag dispatch:
private: void foo(std::true_type /*value_is_string*/) const { /* "partial" */ } void foo(std::false_type /*value_is_string*/) const { /* "base" */ } public: void foo() const { return foo(std::is_same<value, std::string>()); }
or refactoring foo()
base class template partially specialize.
Comments
Post a Comment