Developer knowledge network · moderated exchange

UnreliableCode 커뮤니티

개발자 연구, 리버스 엔지니어링 및 코딩 커뮤니티

Knowledge index살다
4Categories
919Threads
2.8K게시물
Discussion

Why passing parameters by value and std::move is preferred in constructors (Sink Arguments) [StackOverflow Architecture Guide]

raii_clean_coder
Modern C++ Advocate
MEMBER
대표: 190
가입 날짜: Aug 2020
게시물: 20
감사해요: 22
1개월 전 · Jun 29, 2026 11:00 AM
#1

Modern pattern for storing constructor arguments:

CPP
class UserProfile {
    std::string m_name;
    std::vector<int> m_scores;
public:
    // Pass by value, then move into member
    UserProfile(std::string name, std::vector<int> scores)
        : m_name(std::move(name)), m_scores(std::move(scores)) {}
};

If caller passes an rvalue (UserProfile("Alice", {10, 20})), the arguments are constructed once and moved twice with zero deep copies! Avoids having to write separate lvalue and rvalue constructor overloads.

modern_cpp_artisan
C++ Template Wizard
MEMBER
대표: 124
가입 날짜: Jun 2019
게시물: 29
감사해요: 72
1개월 전 · Jun 29, 2026 3:57 PM
#2

Pass-by-value-then-move (the sink parameter idiom) provides the cleanest balance between brevity and move efficiency for constructor parameters.

cpp_concurrency_guru
C++ Standards Expert
MEMBER
대표: 47
가입 날짜: Feb 2018
게시물: 17
감사해요: 75
1개월 전 · Jun 30, 2026 4:18 AM
#3

Saves having to maintain $2^N$ combinatorial overloads for constructors with multiple parameters.