variadic templates - compile error

S

suresh

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 ‘Mystrcat<char*, char*>::init(char*&)’

gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)

Thanks
suresh
 
S

SG

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 ‘Mystrcat<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
 
S

suresh

thank you SG for your code. It compiles and runs correctly. But I am unable to understand the code. Could you kindly explain:
(1) what is forward<T> ?
(2) Why && in the argument?

Thanks again
suresh
 
S

suresh

Hi SG,
thank you very much for the code. But I could not understand this. Could you explain forward <T> and the usage of &&.
thanks
suresh

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



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



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
 
S

SG

thank you SG for your code. It compiles and runs correctly.
But I am unable to understand the code. Could you kindly explain:
(1) what is forward<T> ?
(2) Why && in the argument?

std::forward<> ist a Helper function from the <utility> header that is
supposed to be used for "perfect forwarding". Perfect forwarding is
about forwarding function arguments to another function while
retaining
- the original type
- the original "l/r valueness"

Both properties are (in this case) encoded in the type T and the
forward<T>() call restores these properties, more specifically, the
correct kind of valueness.

Examples:

void foo(const int&); // #1
void foo(int &&); // #2

template<class T> void bar(T&& x) {
foo(forward<T>(x));
}

int main() {
int i = 42;
bar(9); // --> T=int --> T&&=int&& --> #2
bar(i); // --> T=int& --> T&&=int& --> #1
}

SG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top