Thursday, May 10, 2012

template function in template class


template <bool b>
struct A{
  template<typename T> void f(const T& t);
};

template<> template<typename T>
void A<false>::f(const T& t){
}


int main(){
  A<false> a;
  a.f(3);
}

^^^^^^^^^^^^^^^^^^^^^^^^
This does NOT work anymore for partial specialization

template<typename T,bool x>
struct A{

  void f();

};


template<typename T>
void A<T,true>::f(){
}


template<typename T>
void A<T,false>::f(){
}

===COMPILER OUTPUT===



funcSpec.cpp:126:19: error: invalid use of incomplete type ‘struct A<T, true>’
funcSpec.cpp:118:8: error: declaration of ‘struct A<T, true>’
funcSpec.cpp:131:20: error: invalid use of incomplete type ‘struct A<T, false>’
funcSpec.cpp:118:8: error: declaration of ‘struct A<T, false>’



^^^^^^^^^^^^^^^^^^^^^^^
Similarly, you cannot "group" after specializing




template<bool b>
struct A{
  void f();
};

template<>
struct A<true>{
  void f();
};


template<bool b> void A<b>::f(){
}


int main(){
  A<true> a;
  a.f();
};


===COMPILER OUTPUT=====

tmp/ccsJCUcq.o: In function `main':
/home/fabio/research/software/src/helium/tests/funcSpec2.cpp:23: undefined reference to `A<true>::f()'



No comments:

Post a Comment