LinuxQuestions.org
Visit Jeremy's Blog.
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 06-30-2006, 06:53 PM   #1
spursrule
LQ Newbie
 
Registered: Feb 2006
Distribution: LFS
Posts: 26

Rep: Reputation: 15
C++: Specialized templated member functions?


Say I have the following bit of code:

Code:
#include	<iostream>
	using	std::cout;
#include	<vector>
	using	std::vector;

template <class T>
class	foo {
	public:
		void bar() const;
};


template <class T>
void
foo<T>::bar () const
{
	cout << "template generated\n";
}


void
foo<vector<int> >::bar() const
{
	cout << "template specialized\n";
}


int
main(int argc, char *argv[])
{
    foo<int>		f;
    foo<vector<int> >	g;
    f.bar();
    g.bar();

    return	0;
}
Its output is then
Code:
template generated
template specialized

How can I specialize foo<vector<T> >::bar() const?

ie, specialize foo::bar() for objects from templated container types?

Last edited by spursrule; 06-30-2006 at 06:55 PM.
 
Old 07-01-2006, 07:16 PM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
Here's how:
Code:
#include <cstdio>
#include <vector>
using namespace std;

template <typename T>
struct foo { void bar(); };

template <typename T, typename Alloc>
struct foo<vector<T, Alloc> > { void bar(); };

template <typename T>
void foo<T>::bar() { puts("a"); }

template <> template <typename T, typename Alloc>
void foo< vector<T, Alloc> >::bar() { puts("b"); }

int
main() {
    foo<int> f;
    foo<vector <long> > g;
    f.bar();
    g.bar();
}
 
  


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
C++ Templated Class and Borland C++ Compiler enemorales Programming 2 09-25-2010 02:30 PM
C++ - problem with pointers to class member functions Nylex Programming 3 01-15-2006 10:14 AM
Accessing member functions of sockaddr_in Stack Overflow Programming 4 01-27-2005 05:11 PM
c to GCChange of member functions in iostream from GCC296 to Gcc 3.2.2 Basiltp Programming 0 11-29-2004 03:19 PM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM

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

All times are GMT -5. The time now is 12:56 PM.

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