我明白轉發如何完善在通常情況下工作:如果不使用SFINAE或編寫多個版本,是否可以完美地轉發「非泛型」類型?
template <typename T>
void f(T &&arg) {
E(std::forward<T>(arg)));
}
是否有可能完全向前「非通用」類型,如某種形式的std::string
不使用SFINAE或寫多個版本?
/* Likely some kind of template. */
/* template <typename T> */
void f(/* Something goes here*/ arg) {
E(std::forward</* Something goes here. */>(arg);
}
下應該是真實的:
f(std_str_obj); // Lvalue forwarded as const lvalue ref.
f("hello"); // Temporary forwarded as rvalue reference.
f(1); // Error, not a std::string or convertible to std::string.
我懷疑,唯一的辦法是仍編寫一個函數模板,並使用某種形式的SFINAE來限制它(在這種情況下,我可以找出自己的東西),但我想知道是否有一些簡單的方法,我錯過了。
'void f(std :: string && arg){E(std :: forward(arg));}'應該這樣做。 –
@RSahu:那不會接受一個左值。 –
'void f(std :: string arg)'應該這樣做。 –