I am new to template function with variadic arguments. The code , inspired by a Codeview article , don't compile at the CreateBook(), need help.
I want to create an object using factory type method using template method.
using namespace std;
template<class T>
class bar
{
public:
template<class... Args>
static bar<T> CreateBook(Args&&... args)
{
return bar<T>(std::forward<Args>(args)...); // Don't compile here
}
T get()
{
return val;
}
private:
bar(const T& t) :val(t) {}
T val;
};
struct book
{
string name;
int phone;
book(string s, int t) : name(s), phone(t) {}
};
void print(bar<book> k)
{
cout << k.get().name << " " << k.get().phone << endl;
}
int main()
{
bar<book> b = bar<book>::CreateBook("Hello World",91520);
print(b);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire