在C++98
中椒涯,按值傳遞(pass-by-value)意味著低效的、不必要的拷貝回梧,被廣大程序員嗤之以鼻废岂。按照慣例,對于自定義類型狱意,如果用于入?yún)t應(yīng)使用pass-by-const-reference
湖苞;如果用于出參則應(yīng)pass-by-reference
。
在缺失移動語義(move semantics)
下的C++98
標(biāo)準(zhǔn)详囤,這樣的結(jié)論無可厚非财骨。但是,在增加移動語義(move semantics)
下的C++11
標(biāo)準(zhǔn)藏姐,按值傳遞在某些特殊場景具有優(yōu)異的表現(xiàn)力隆箩,是時候為"按值傳遞"正名了。
一個例子
存在兩個使用std::function
定義的「仿函數(shù)」羔杨。其中捌臊,std::function
的泛型參數(shù)使用函數(shù)的原型表達(dá)。例如std::function<bool(int)>
的泛型參數(shù)是一個一元的謂詞兜材。
#include <string>
#include <functional>
using Matcher = std::function<bool(int)>;
using Action = std::function<std::string(int)>;
C++98風(fēng)格
存在一個Atom
的函數(shù)對象理澎。按照既有的C++98
的習(xí)慣,使用pass-by-const-reference
傳遞參數(shù)护姆,然后將它們拷貝至私有數(shù)據(jù)區(qū)矾端。此時必然調(diào)用std::function
的「拷貝構(gòu)造函數(shù)」。
struct Atom {
Atom(const Matcher& matcher, const Action& action)
: matcher(matcher), action(action) {
}
std::string operator()(int m) const {
return matcher(m) ? action(m) : "";
}
private:
Matcher matcher;
Action action;
};
可是卵皂,當(dāng)傳遞給Atom
構(gòu)造函數(shù)的是「右值」時秩铆,我們期望調(diào)用「移動構(gòu)造函數(shù)」,而非「拷貝構(gòu)造函數(shù)」;因為對于std::function
殴玛,移動構(gòu)造相對拷貝構(gòu)造更加低廉捅膘。一般地,針對這個問題滚粟,存在3種解決方案寻仗,我們逐一分析。
重載函數(shù)
使用重載的構(gòu)造函數(shù)凡壤,可以準(zhǔn)確的根據(jù)左值或右值實現(xiàn)函數(shù)指派署尤。但是,為了支持兩個參數(shù)的重載亚侠,便要實現(xiàn)4個重載的構(gòu)造函數(shù)曹体,可謂得不償失。組合爆炸是一種典型的設(shè)計缺陷硝烂,為了提升程序性能箕别,該方案所付出的成本相當(dāng)昂貴。
struct Atom {
Atom(const Matcher& matcher, const Action& action)
: matcher(matcher), action(action) {
}
Atom(const Matcher& matcher, Action&& action)
: matcher(matcher), action(std::move(action)) {
}
Atom(Matcher&& matcher, const Action& action)
: matcher(std::move(matcher)), action(action) {
}
Atom(Matcher&& matcher, Action&& action)
: matcher(std::move(matcher)), action(std::move(action)) {
}
std::string operator()(int m) const {
return matcher(m) ? action(m) : "";
}
private:
Matcher matcher;
Action action;
};
成本分析
當(dāng)傳遞左值滞谢,存在一次「拷貝構(gòu)造」串稀;當(dāng)傳遞右值,存在一次「移動構(gòu)造」。但是,存在不可接受的組合爆炸的問題蹋订。
透傳引用
「透傳引用(Forward Reference)」是C++11
實現(xiàn)「完美轉(zhuǎn)換(Perfect Forward)」機(jī)制而引入的一個概念。使用「透傳引用」可以合并上述4個構(gòu)造函數(shù)微酬,實現(xiàn)左值和右值的透明傳遞和分發(fā)。但是颤陶,使用「透傳引用」引入了泛型設(shè)計,其實現(xiàn)必須放到頭文件陷遮,增加了編譯時依賴的成本滓走,極大地增加了實現(xiàn)的復(fù)雜度。而且帽馋,因為模板而導(dǎo)致代碼膨脹的問題搅方,相對上述重載方案其目標(biāo)代碼規(guī)模并沒有得到改善,甚至更糟绽族。此外姨涡,鑒于「完美轉(zhuǎn)換」并非100%的完美,當(dāng)客戶傳遞不正確的類型時吧慢,編譯器的錯誤信息相當(dāng)冗長涛漂。
struct Atom {
template <typename Matcher, typename Action>
Atom(Matcher&& matcher, Action&& action)
: matcher(std::forward<Matcher>(matcher))
, action(std::forward<Action>(action)) {
}
std::string operator()(int m) const {
return matcher(m) ? action(m) : "";
}
private:
Matcher matcher;
Action action;
};
成本分析
當(dāng)傳遞左值,存在一次「拷貝構(gòu)造」;當(dāng)傳遞右值匈仗,存在一次「移動構(gòu)造」瓢剿。避免了組合爆炸的問題,但引入了模板的復(fù)雜度悠轩,及其代碼膨脹的問題间狂。但是,相對重載方法火架,代碼實現(xiàn)更加簡潔鉴象,更加緊湊。
按值傳遞
考慮使用pass-by-value
的方式傳遞Matcher
和Action
何鸡,不僅實現(xiàn)簡單纺弊,天然支持左值或右值,而且成本在接受的范圍之內(nèi)音比。
struct Atom {
Atom(Matcher matcher, Action action)
: matcher(std::move(matcher))
, action(std::move(action)) {
}
std::string operator()(int m) const {
return matcher(m) ? action(m) : "";
}
private:
Matcher matcher;
Action action;
};
成本分析
當(dāng)傳遞左值俭尖,存在一次「拷貝構(gòu)造」,及其一次「移動構(gòu)造」洞翩;當(dāng)傳遞右值稽犁,存在兩次次「移動構(gòu)造」。相對上兩個方案骚亿,兩種場景其成本均多了一次低廉的「移動構(gòu)造」已亥。但是,該方案完全避免了組合爆炸的問題来屠,及其模板的復(fù)雜度虑椎,代碼最為簡潔。
何時按值傳遞
綜上述俱笛,可以歸納如下條件捆姜,可以考慮使用「按值傳遞」的方法。
- 產(chǎn)生副本迎膜;
- 類型可拷貝泥技;
- 移動成本低廉。
例如磕仅,上述的函數(shù)對象Atom
珊豹,完全滿足上述必要條件。
- 產(chǎn)生副本:通過「拷貝構(gòu)造」或「移動構(gòu)造」榕订,產(chǎn)生私有的
matcher
和action
副本店茶; - 類型可拷貝:
std::function
類型可拷貝。 - 移動成本低廉:
std::function
的移動構(gòu)造的成本非常低廉劫恒。
在Lambda中的應(yīng)用
事實上贩幻,C++98風(fēng)格的函數(shù)對象,都可以使用Lambda簡化實現(xiàn)。例如段直,原來的函數(shù)對象Atom
實現(xiàn)吃溅,可以使用更為簡潔的Lambda表達(dá)式實現(xiàn)。
using Rule = std::function<std::string(int)>;
Rule atom(Matcher matcher, Action action) {
return [matcher, action](int m) {
return matcher(m) : action(m) : "";
};
}
但是鸯檬,在Lambda表達(dá)式的「參數(shù)捕獲列表」中决侈,matcher, action
是按值傳遞給閉包對象的,此時調(diào)用的是std::function
的「拷貝構(gòu)造函數(shù)」喧务。幸運的是赖歌,C++14
支持以初始化的方式將對象移動至閉包之中,彌補(bǔ)了這個遺憾功茴。
using Rule = std::function<std::string(int)>;
Rule atom(Matcher matcher, Action action) {
return [matcher = std::move(matcher), action = std::move(action)](int m) {
return matcher(m) : action(m) : "";
};
}