LinuxQuestions.org
Visit Jeremy's Blog.
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, 03: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 04:09 AM.
 
Old 09-08-2010, 07:11 AM   #2
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,139

Rep: Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127
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 07:12 AM.
 
1 members found this post helpful.
Old 09-08-2010, 09:55 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
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, 10: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, 10:56 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 03:20 PM
Standard Template Library Reference (std::string) lucky6969b Programming 3 03-15-2006 04:09 PM
Can't use template with std::map astorm Programming 3 11-03-2005 07:55 PM
how to sort std::vector<double>? markhod Programming 4 10-12-2005 05:21 AM
std::vector <> ::max_size ta0kira Programming 1 04-29-2005 05:59 AM


All times are GMT -5. The time now is 02:57 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration