LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-08-2010, 02:50 AM   #1
CartmanYO
LQ Newbie
 
Registered: Jun 2009
Posts: 27

Rep: Reputation: 1
Template algorithm for std::vector and std::string


Hello! For example, I have two functions:
Quote:
void func1(std::string &s)
{
s.clear();
}

void func2(std::vector &v)
{
v.clear();
}
Is there a way to create one template function for both vector and string that would clear them?

Last edited by CartmanYO; 09-08-2010 at 03:09 AM.
 
Old 09-08-2010, 06:11 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by CartmanYO View Post
For example, I have two functions:
Please use CODE tags, not QUOTE tags for the code you post.

Quote:
Is there a way to create one template function for both vector and string that would clear them?
You can make one template function for all classes. That function would work for any classes that have clear() methods and give compile time errors for those that don't.

That is the usual way that templates work in C++.

Code:
template <class Anything>
void func1(Anything &s)
{
   s.clear();
}

Last edited by johnsfine; 09-08-2010 at 06:12 AM.
 
1 members found this post helpful.
Old 09-08-2010, 08:55 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by johnsfine View Post
Please use CODE tags, not QUOTE tags for the code you post.



You can make one template function for all classes. That function would work for any classes that have clear() methods and give compile time errors for those that don't.

That is the usual way that templates work in C++.

Code:
template <class Anything>
void func1(Anything &s)
{
   s.clear();
}
If I understand correctly, your template only covers 'func1' while the OP has two different functions: 'func1', 'func2'.

The problem can, of course, be resolved trivially using a macro, but the OP requested a template solution.
 
Old 09-08-2010, 09:54 AM   #4
CartmanYO
LQ Newbie
 
Registered: Jun 2009
Posts: 27

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by johnsfine View Post
Please use CODE tags, not QUOTE tags for the code you post.



You can make one template function for all classes. That function would work for any classes that have clear() methods and give compile time errors for those that don't.

That is the usual way that templates work in C++.

Code:
template <class Anything>
void func1(Anything &s)
{
   s.clear();
}
Thanks, johnsfine. I didn't know that templates make it possible to call methods from "Anything" classes.
 
Old 09-08-2010, 09:56 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by CartmanYO View Post
Thanks, johnsfine. I didn't know that templates make it possible to call methods from "Anything" classes.
The class is not "Anything"; "Anything" is a parameter to be replaced with actual class/type name during instantiation.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Vector 5.8 std Mouse setup help sickley VectorLinux 2 01-09-2007 02:20 PM
Standard Template Library Reference (std::string) lucky6969b Programming 3 03-15-2006 03:09 PM
Can't use template with std::map astorm Programming 3 11-03-2005 06:55 PM
how to sort std::vector<double>? markhod Programming 4 10-12-2005 04:21 AM
std::vector <> ::max_size ta0kira Programming 1 04-29-2005 04:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration