LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-16-2008, 12:00 AM   #1
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
c++ template class constructors and references


That title was a mouthfull! Okay, so here's my problem. This c++ code will compile:
Code:
template<typename T>
class test{
public:
  test(T foo){}
};

int main(int argc, char** argv)
{
  test<int> i(2);
  return 0;
}
But this code will not:
Code:
template<typename T>
class test{
public:
  test(T& foo){}
};

int main(int argc, char** argv)
{
  test<int> i(2);
  return 0;
}
The only difference is that one constructor passes by reference and the other by value. Why can't I pass that by reference? Is this a compiler bug or the desired result? I'm using the Debian g++ 4.2.1 on Lenny on x86.
 
Old 06-16-2008, 01:11 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
The problem is you're passing an int to the constructor. I don't think you can pass integer literals by references, which may be because they're not stored in memory (I don't know for sure, actually). You can do what you want with an extra variable, e.g.

int x = 2;
test<int> i(&x);
 
Old 06-16-2008, 01:17 AM   #3
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148

Original Poster
Blog Entries: 1

Rep: Reputation: 48
Ah, okay. Yep, that should've been obvious to me :-) Thanks much.
 
Old 06-16-2008, 01:41 AM   #4
LinuxManMikeC
Member
 
Registered: Nov 2007
Location: Provo, Utah
Distribution: Debian and Ubuntu
Posts: 74

Rep: Reputation: 15
You can assign a literal to a const reference. Of course this prevents you from changing the value being referenced. In this case, constructors usually only alter the object they're instantiating, so you can probably get by using a const reference. Though keep in mind that if the template class you are making is often specialized with scalars (or really small data structures) then you aren't getting a benefit (or possibly hurting performance) passing by reference and would be better off passing by value.
 
Old 06-16-2008, 07:12 AM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
The reason your code has a problem is due to left hand side and right hand side values ( see reference). The constructor is defined as taking a lhs value yet you are supplying a rhs value, as has been mentioned adding const makes the constructor take a non modifiable lhs value(constant qualified type) of which the input parameter is.
 
Old 06-17-2008, 08:52 AM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
I should add that C++0x adds some features to rhs values and introduces a new type which is the right hand side reference, so something like the following "may" be possible with the new standard.
Code:
template<typename T>
class test{
public:
  test(T&& foo){}
};

int main(int argc, char** argv)
{
  test<int> i(std::move(2) );
  return 0;
}
http://www.open-std.org/jtc1/sc22/wg...006/n2027.html
 
  


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
Specialized template class constructors a headache... The_Nerd Programming 8 06-08-2007 08:13 AM
Undefined references to vtable in class constructor? RavenOfOdin Programming 2 03-01-2006 06:46 PM
Template class with a template member... Nicholas Bishop Programming 3 02-21-2005 09:27 PM
template class ckcheung0927 Programming 3 11-28-2004 04:59 PM
C++ - throwing exceptions in derived class constructors? MadCactus Programming 4 08-09-2004 07:29 AM

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

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