0

I searched a bit couldnt find an answer to this exact query.

I will directly show what I need , using code

template<typename T1,typename T2>
class C1
{
    private:
     void foo(T1 a,T1 b)
     {
       //implementation
     }    
}

Now I need to specialize foo when T1 is std::string and T2 is of any type , I tried like below and it doesnt seem to work

template<typename T2>
void C1<std::string,typename T2>::foo(std::string a,std::string b)
{
  //something different implementation
}

So is this allowed as per standards? and if yes how to achieve it? if no what is the best way to do it?

Edit : The error reported by the compiler is as below

/tmp/P9Vp19KuKH.cpp:28:32: error: template argument 2 is invalid
   28 | void C1<std::string,typename T2>::foo(std::string a,std::string b)
      |                                ^
bsguru
  • 432
  • 4
  • 21
  • 1
    Can you show us a [mcve] (i.e. how you're applying it, and the verbatim error message) please? – πάντα ῥεῖ Mar 11 '21 at 16:22
  • 2
    Seem like an xy problem. I'm sure there are better ways to do what you want to do. I'm saying that because there is no such thing as a partial specialization of a function. – Guillaume Racicot Mar 11 '21 at 16:27
  • Maybe a SFINAE is the solution to your problem: https://godbolt.org/z/s5j1o6 – m88 Mar 11 '21 at 16:41
  • This is something I am hearing for first time and will definitely check it out and I need some time to digest it ;) , do you have any recommended resource to that topic? – bsguru Mar 11 '21 at 16:54
  • Well, I'd say check the cppreference pages [std::enable_if](https://en.cppreference.com/w/cpp/types/enable_if) and [SFINAE](https://en.cppreference.com/w/cpp/language/sfinae) but I'm not sure if it's really didactic... – m88 Mar 11 '21 at 17:05
  • 1
    @m88 you do not need the `enable_if` there https://godbolt.org/z/sceEjK – Aykhan Hagverdili Mar 11 '21 at 17:48
  • This is even concise , thanks for this! – bsguru Mar 12 '21 at 02:29

0 Answers0