Re: variadic templates - compile error
On 19 Aug., 01:31, suresh <suresh.amritap...@gmail.com> wrote:
Hi,
Could you help me in fixing the compile error in the following code:
#include <sstream>
#include <iostream>
using namespace std;
template<typename T, typename ...P>
class Mystrcat{
public:
Mystrcat(T t, P... p){init(t,p...);}
ostringstream & get(){return o;}
private:
ostringstream o;
void init(){}
void init(T t, P... p);
};
template<typename T, typename ...P>
void Mystrcat<T,P...>::init(T t, P ...p){
o << t;
if (sizeof...(p)) init(p...);
}
int main(){
Mystrcat<char*,char *> m("Amma","Namasivayah");
cout << m.get().str();
}
I get the error, no matching function for call to =91Mystrcat<char*, char=
*>::init(char*&)'
Your Mystrcat<char*,char*> ohly has two init functions:
- void init()
- void init(char*,char*)
and inside the second one, you're trying to call some nonexistinc init
with just a single parameter.
Also, I fail to see the benefit of a class here. Why not simply:
inline void strcat_helper(ostream & os) {} // nothing to do
template<class T, class... More>
inline void strcat_helper(ostream & os, T && t, More &&... more)
{
os << forward<T>(t);
strcat_helper(os,forward<More>(more)...);
}
template<class... Args>
string strcat(Args &&... args)
{
ostringstream os;
strcat_helper(os,forward<Args>(args)...);
return os.str();
}
SG