SlideShare a Scribd company logo
Version of 2015.07.10-2
Hot C++:
New Style of Arguments Passing
Andrey Upadyshev
Licensed under CC BY-SA 4.0 License
1
Move Semantics Changes It All
✤ Moving is faster than copying and we want this
performance gain!
✤ Template code has perfect forwarding, but non-template
has nothing.
✤ We need to consider the best way to pass a movable
object into a non-template function.
2
Problem
Typical function:
void foo(const std::string& arg);
OK if foo implementation does not copy argument
internally.
But if the implementation needs to copy argument, it
loses the opportunity to move from user provided rvalue
and does a copy anyway.
3
Straightforward Solution
Two overloads:
void foo(const std::string& arg) {

std::string t(arg); // copy

...

}
void foo(std::string&& arg) {

std::string t(std::move(arg)); // move

...

}
Not very elegant though: two near identical
implementations of the same thing.
4
Problem Goes Deeper
struct Bar {

Bar(const std::string& a,

const std::string& b,

const std::string& c)

: a_(a), b_(b), c_(c) {}



private:

std::string a_, b_, c_;

};
Straightforward solution is 2^3 overloads (2^N for
general case). Too many, isn't it?
5
"Pass ByValue" Solution
Foo::Foo(std::string arg)

: m_arg(std::move(arg))

{}
Main point:
1. If called with rvalue then it is moved into argument

else (i.e. called with lvalue) one is copied into
argument.
2. Argument is moved further.
6
When Rvalue Passed
Foo::Foo(std::string arg)

: m_arg(std::move(arg))

{}
std::string readFile();

Foo foo1(readFile());
One move (thanks to copy elision)
std::string bar;

...

Foo foo2(std::move(bar));
Two moves
[Just] one extra move in the worst case comparing to
straightforward solution.
7
When Lvalue Passed
Foo::Foo(std::string arg)

: m_arg(std::move(arg))

{}
const std::string& bar();

Foo foo(bar());
One copy and one move
[Just] one extra move in the worst case comparing to
straightforward solution.
8
Good Expansibility
struct Bar {

Bar(std::string a,

std::string b,

std::string c)

: a_(std::move(a))

, b_(std::move(b))

, c_(std::move(c))

{}



private:

std::string a_, b_, c_;

};
It’s always a single function regardless the number of
arguments.
9
Pass ByValue, Return ByValue
Vector sorted(Vector arg) {

std::sort(arg.begin(), arg.end());

return arg;

}
“Pass by value” approach is seamlessly works with “return by
value” one (Latter is a default one for C++11):
10
arg is moved on return.
Note that RVO can’t be applied to
a function argument
Pass ByValue Is Not Perfect
✤ Not a “Swiss knife” like perfect forwarding: applicable only to types
that are copyable and cheap movable.
✤ Even one extra move is not always acceptable
✤ Interface of the function depends on implementation.
✤ Solution: pass by value if function conceptually needs to make a copy.
✤ Not to be used with base type because of slicing.
✤ Unnecessary copies if function needs a copy not every time. (Perhaps,
such function asks for the refactoring :)
✤ Strange exception guarantee: a function itself is non-throw (move
usually does not throw) but a function call throws because of argument
copy.
11
Pass ByValue Is Not Perfect
✤ Fresh copy + move assignment [1] can be significantly more expensive
than pass by ref + copy assignment [2]. (Think about copy assigning to
string that already has a large enough buffer allocated):
void Foo::setName(std::string name) { // [1]

m_name = std::move(name); // [Always] destroy existing

// string during move

}
foo.setName(meow); // [Always] allocate and copy bytes
vs
void Foo::setName(const std::string& name) { // [2]

m_name = name; // If existing string is big enough, 

// just copy bytes w/o realloc

}
foo.setName(meow); // Do nothing
12
Pass ByValue Is Not Perfect, But…
✤ Nearly as efficient as pass by reference
✤ Easier to implement comparing to both other solutions
(set of overloaded functions and template function
accepting universal references)
✤ Generates less object code (arguable, depends on inlining)
✤ Despite it’s drawbacks which need to be always
considered, it is a worth to use solution.
13
The Algorithm
Does the [non-template]
function conceptually make a copy of a
[movable] argument?
Pass by const reference
Pass by value
Yes
No
Based on the profiler result or other
evidence, optimize by providing
lvalue/rvalue overloads.
14
Useful Links
Boris Kolpackov, Efficient argument passing in C++11

http://codesynthesis.com/~boris/blog/2012/06/19/efficient-
argument-passing-cxx11-part1/
Dave Abrahams, Want Speed? Pass by Value.

http://cpp-next.com/archive/2009/08/want-speed-pass-by-
value/
Scott Meyers, Effective Modern C++, Item 41
15
Discussion?
16

More Related Content

PDF
[OLD VERSION, SEE DESCRIPTION FOR THE NEWER VERSION LINK] Hot С++: Universal ...
PDF
[OLD VERSION, SEE DESCRIPTION FOR NEWER VERSION LINK] Hot C++: Rvalue Referen...
PDF
Hot C++: Rvalue References And Move Semantics
PDF
Hot С++: Universal References And Perfect Forwarding
PDF
C++11: Rvalue References, Move Semantics, Perfect Forwarding
PDF
Where to type_std_move?
PDF
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
PPTX
[OLD VERSION, SEE DESCRIPTION FOR THE NEWER VERSION LINK] Hot С++: Universal ...
[OLD VERSION, SEE DESCRIPTION FOR NEWER VERSION LINK] Hot C++: Rvalue Referen...
Hot C++: Rvalue References And Move Semantics
Hot С++: Universal References And Perfect Forwarding
C++11: Rvalue References, Move Semantics, Perfect Forwarding
Where to type_std_move?
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...

What's hot (20)

PPTX
C++ 11 Features
PDF
Smart Pointers in C++
PDF
Regular types in C++
PPT
What's New in C++ 11?
PPTX
Modern C++
PDF
Modern C++
PPT
Gentle introduction to modern C++
PPTX
C traps and pitfalls for C++ programmers
PDF
06 -working_with_strings
PPTX
The Style of C++ 11
PPTX
Summary of C++17 features
PPTX
C++11: Feel the New Language
PPTX
Introduction to c programming
PDF
String notes
PPT
String & its application
PPTX
C++ Presentation
PDF
C++ references
PPTX
Implementation Of String Functions In C
PPTX
C programming - String
PPTX
C++ 11 Features
Smart Pointers in C++
Regular types in C++
What's New in C++ 11?
Modern C++
Modern C++
Gentle introduction to modern C++
C traps and pitfalls for C++ programmers
06 -working_with_strings
The Style of C++ 11
Summary of C++17 features
C++11: Feel the New Language
Introduction to c programming
String notes
String & its application
C++ Presentation
C++ references
Implementation Of String Functions In C
C programming - String
Ad

Similar to Hot C++: New Style of Arguments Passing (20)

PPTX
C++11 move semantics
PDF
Modern C++ Explained: Move Semantics (Feb 2018)
PDF
C++ 11 usage experience
PDF
C++11 talk
PDF
(4) cpp abstractions references_copies_and_const-ness
PDF
(4) cpp automatic arrays_pointers_c-strings
PPTX
Function Overloading Call by value and call by reference
PPTX
6. Functions in C ++ programming object oriented programming
PPTX
OOP-Module-1-Section-4-LectureNo1-5.pptx
PDF
Stringing Things Along
PPTX
Beginning direct3d gameprogrammingcpp02_20160324_jintaeks
DOCX
Functions in c++
PPTX
Lecture 4, c++(complete reference,herbet sheidt)chapter-14
PPTX
Introduction to pointers in c plus plus .
PPTX
Pointers in C++ object oriented programming
PPTX
2CPP13 - Operator Overloading
PPTX
FUNCTIONS, CLASSES AND OBJECTS.pptx
PPTX
Call by value or call by reference in C++
PPTX
Summary of effective modern c++ item1 2
PPTX
Classes function overloading
C++11 move semantics
Modern C++ Explained: Move Semantics (Feb 2018)
C++ 11 usage experience
C++11 talk
(4) cpp abstractions references_copies_and_const-ness
(4) cpp automatic arrays_pointers_c-strings
Function Overloading Call by value and call by reference
6. Functions in C ++ programming object oriented programming
OOP-Module-1-Section-4-LectureNo1-5.pptx
Stringing Things Along
Beginning direct3d gameprogrammingcpp02_20160324_jintaeks
Functions in c++
Lecture 4, c++(complete reference,herbet sheidt)chapter-14
Introduction to pointers in c plus plus .
Pointers in C++ object oriented programming
2CPP13 - Operator Overloading
FUNCTIONS, CLASSES AND OBJECTS.pptx
Call by value or call by reference in C++
Summary of effective modern c++ item1 2
Classes function overloading
Ad

Recently uploaded (20)

PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Transform Your Business with a Software ERP System
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Designing Intelligence for the Shop Floor.pdf
PPT
Introduction Database Management System for Course Database
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
System and Network Administration Chapter 2
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
medical staffing services at VALiNTRY
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Nekopoi APK 2025 free lastest update
PPTX
Introduction to Artificial Intelligence
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Computer Software and OS of computer science of grade 11.pptx
PTS Company Brochure 2025 (1).pdf.......
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Transform Your Business with a Software ERP System
Odoo Companies in India – Driving Business Transformation.pdf
Designing Intelligence for the Shop Floor.pdf
Introduction Database Management System for Course Database
Wondershare Filmora 15 Crack With Activation Key [2025
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
wealthsignaloriginal-com-DS-text-... (1).pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
2025 Textile ERP Trends: SAP, Odoo & Oracle
System and Network Administration Chapter 2
CHAPTER 2 - PM Management and IT Context
medical staffing services at VALiNTRY
Operating system designcfffgfgggggggvggggggggg
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Nekopoi APK 2025 free lastest update
Introduction to Artificial Intelligence
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises

Hot C++: New Style of Arguments Passing

  • 1. Version of 2015.07.10-2 Hot C++: New Style of Arguments Passing Andrey Upadyshev Licensed under CC BY-SA 4.0 License 1
  • 2. Move Semantics Changes It All ✤ Moving is faster than copying and we want this performance gain! ✤ Template code has perfect forwarding, but non-template has nothing. ✤ We need to consider the best way to pass a movable object into a non-template function. 2
  • 3. Problem Typical function: void foo(const std::string& arg); OK if foo implementation does not copy argument internally. But if the implementation needs to copy argument, it loses the opportunity to move from user provided rvalue and does a copy anyway. 3
  • 4. Straightforward Solution Two overloads: void foo(const std::string& arg) {
 std::string t(arg); // copy
 ...
 } void foo(std::string&& arg) {
 std::string t(std::move(arg)); // move
 ...
 } Not very elegant though: two near identical implementations of the same thing. 4
  • 5. Problem Goes Deeper struct Bar {
 Bar(const std::string& a,
 const std::string& b,
 const std::string& c)
 : a_(a), b_(b), c_(c) {}
 
 private:
 std::string a_, b_, c_;
 }; Straightforward solution is 2^3 overloads (2^N for general case). Too many, isn't it? 5
  • 6. "Pass ByValue" Solution Foo::Foo(std::string arg)
 : m_arg(std::move(arg))
 {} Main point: 1. If called with rvalue then it is moved into argument
 else (i.e. called with lvalue) one is copied into argument. 2. Argument is moved further. 6
  • 7. When Rvalue Passed Foo::Foo(std::string arg)
 : m_arg(std::move(arg))
 {} std::string readFile();
 Foo foo1(readFile()); One move (thanks to copy elision) std::string bar;
 ...
 Foo foo2(std::move(bar)); Two moves [Just] one extra move in the worst case comparing to straightforward solution. 7
  • 8. When Lvalue Passed Foo::Foo(std::string arg)
 : m_arg(std::move(arg))
 {} const std::string& bar();
 Foo foo(bar()); One copy and one move [Just] one extra move in the worst case comparing to straightforward solution. 8
  • 9. Good Expansibility struct Bar {
 Bar(std::string a,
 std::string b,
 std::string c)
 : a_(std::move(a))
 , b_(std::move(b))
 , c_(std::move(c))
 {}
 
 private:
 std::string a_, b_, c_;
 }; It’s always a single function regardless the number of arguments. 9
  • 10. Pass ByValue, Return ByValue Vector sorted(Vector arg) {
 std::sort(arg.begin(), arg.end());
 return arg;
 } “Pass by value” approach is seamlessly works with “return by value” one (Latter is a default one for C++11): 10 arg is moved on return. Note that RVO can’t be applied to a function argument
  • 11. Pass ByValue Is Not Perfect ✤ Not a “Swiss knife” like perfect forwarding: applicable only to types that are copyable and cheap movable. ✤ Even one extra move is not always acceptable ✤ Interface of the function depends on implementation. ✤ Solution: pass by value if function conceptually needs to make a copy. ✤ Not to be used with base type because of slicing. ✤ Unnecessary copies if function needs a copy not every time. (Perhaps, such function asks for the refactoring :) ✤ Strange exception guarantee: a function itself is non-throw (move usually does not throw) but a function call throws because of argument copy. 11
  • 12. Pass ByValue Is Not Perfect ✤ Fresh copy + move assignment [1] can be significantly more expensive than pass by ref + copy assignment [2]. (Think about copy assigning to string that already has a large enough buffer allocated): void Foo::setName(std::string name) { // [1]
 m_name = std::move(name); // [Always] destroy existing
 // string during move
 } foo.setName(meow); // [Always] allocate and copy bytes vs void Foo::setName(const std::string& name) { // [2]
 m_name = name; // If existing string is big enough, 
 // just copy bytes w/o realloc
 } foo.setName(meow); // Do nothing 12
  • 13. Pass ByValue Is Not Perfect, But… ✤ Nearly as efficient as pass by reference ✤ Easier to implement comparing to both other solutions (set of overloaded functions and template function accepting universal references) ✤ Generates less object code (arguable, depends on inlining) ✤ Despite it’s drawbacks which need to be always considered, it is a worth to use solution. 13
  • 14. The Algorithm Does the [non-template] function conceptually make a copy of a [movable] argument? Pass by const reference Pass by value Yes No Based on the profiler result or other evidence, optimize by providing lvalue/rvalue overloads. 14
  • 15. Useful Links Boris Kolpackov, Efficient argument passing in C++11
 http://codesynthesis.com/~boris/blog/2012/06/19/efficient- argument-passing-cxx11-part1/ Dave Abrahams, Want Speed? Pass by Value.
 http://cpp-next.com/archive/2009/08/want-speed-pass-by- value/ Scott Meyers, Effective Modern C++, Item 41 15