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 04-23-2008, 03:06 AM   #1
neutrino17
LQ Newbie
 
Registered: Apr 2008
Posts: 5

Rep: Reputation: 0
C++ operator= specialization


I have a class template parametrized by one typename. I also want to overload its operator= for common case and for double in particular. So I write this code:

Code:
template <typename T>
class field {
    field() {}
    ~field() {}
public:
    T val;
    field<double>& operator =(field<double>& a);
    field<T>& operator =(field<T>& a);
};

template<typename T>
field<T>& field<T>::operator =(field<T>& a)
{
//  common code
    return a;
}
template<>
field<double>& field<double>::operator =(field<double>& a)
{
//  double-specific code
    return a;
}
But g++ says it can not compile that:
Quote:
test.cpp: In instantiation of ‘field<double>’:
test.cpp:23: instantiated from here
test.cpp:17: error: ‘field<T>& field<T>:perator=(field<T>&) [with T = double]’ cannot be overloaded
test.cpp:12: error: with ‘field<double>& field<T>:perator=(field<double>&) [with T = double]’
What should I do? I want to be able to run specific code for field<double>:perator=.
 
Old 04-23-2008, 05:49 AM   #2
LinuxManMikeC
Member
 
Registered: Nov 2007
Location: Provo, Utah
Distribution: Debian and Ubuntu
Posts: 74

Rep: Reputation: 15
Template overloading doesn't work quite the same as function overloading. You don't need to declare the specialized version of the member function in the class definition, only the most generic. Then you just write the specialized implementation as you already have done.

Code:
template <typename T>
class field {
    field() {}
    ~field() {}
public:
    T val;

    //This is wrong, delete!!!
    //field<double>& operator =(field<double>& a);

    field<T>& operator =(field<T>& a);
};

template<typename T>
field<T>& field<T>::operator =(field<T>& a)
{
//  common code
    return a;
}

//This is all you need to create the specialization
template<>
field<double>& field<double>::operator =(field<double>& a)
{
//  double-specific code
    return a;
}

Last edited by LinuxManMikeC; 04-23-2008 at 05:51 AM.
 
Old 04-23-2008, 06:02 AM   #3
neutrino17
LQ Newbie
 
Registered: Apr 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Ok, I got it. Thanks.
 
  


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
$#^!% template specialization headache! The_Nerd Programming 3 04-27-2007 03:45 PM
Suitable Specialization course for Msc Computer Science student in India anoopkjm Linux - Certification 2 11-25-2006 07:31 AM
What is the operator * ./. . . * royeo Linux - Newbie 2 09-03-2006 06:13 PM
C++ operator += uman Programming 1 02-20-2005 04:37 PM
Explicit specialization in non namespace scope ashwinipahuja Programming 3 06-04-2004 07:16 AM

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

All times are GMT -5. The time now is 05:19 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