LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-18-2009, 01:44 PM   #1
lyle_s
Member
 
Registered: Jul 2003
Distribution: Slackware
Posts: 392

Rep: Reputation: 55
C++ splice a list returned from a function


Code:
#include <list>
#include <string>
using namespace std;

list<string> return_a_list();


int main() {
	list<string> LS1;

	LS1.push_back("element 1 from LS1");
	LS1.push_back("element 2 from LS1");
	LS1.push_back("element 3 from LS1");
	LS1.push_back("element 4 from LS1");
	

	// This works...
	list<string> temp = return_a_list();
	LS1.splice(LS1.end(), temp);

	// ...then why can't I do this???
	LS1.splice(LS1.end(), return_a_list());
	
}

list<string> return_a_list() {
	list<string> return_list;
	return_list.push_back("element 1 from return_a_list");
	return_list.push_back("element 2 from return_a_list");
	return return_list;
}
The compile error from http://www.comeaucomputing.com/tryitout is, "no instance of overloaded function "list<string>::splice"".

Just a curiosity, but aren't 'return_a_list()' and 'temp' the same type?

Lyle.
 
Old 11-18-2009, 01:47 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
The second argument must be a non-const lvalue because it must be able to be modified. You can't use a non-const reference to a temporary.
Kevin Barry

PS "lvalue" essentially means the structure must have a definite location and it must be complete (think of the left side of an assignment.) An rvalue (think of the right side of an assignment,) on the other hand, could be only the parts of the structure necessary for the next operation, e.g. return_a_list().size() might be optimized so that only the size is copied when return_a_list returns. It's a nuance of the C++ standard. Think of it as the difference between x being equal to 2 and 2 itself.

Last edited by ta0kira; 11-18-2009 at 02:01 PM.
 
Old 11-18-2009, 02:04 PM   #3
lyle_s
Member
 
Registered: Jul 2003
Distribution: Slackware
Posts: 392

Original Poster
Rep: Reputation: 55
I obviously need to brush up on lvalues/rvalues.

Your explanation will at the very least get me started.

Thanks!

Lyle.
 
  


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
bash: list vars and function CarLost Programming 3 02-21-2009 01:02 PM
Bash Commands List by Function? TBennettcc Linux - Newbie 6 01-06-2006 02:08 AM
Delete Linked List Function Mistro116@yahoo.com Programming 6 12-10-2005 02:43 PM
How to rm a list of files returned from another program? neo_in_matrix Linux - Newbie 2 04-12-2005 11:24 PM
C++, using custom function to sort an std::list R00ts Programming 3 01-03-2005 09:41 AM

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

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