See the piece of code below(notice that s is a array with char instead of string):
#include <string>
#include <iostream>
#include <utility>
void func(std::string & s, char a) {
std::cout << "here1" << std::endl;
// ...
}
void func(std::string && s, char a) {
std::cout << "here2" << std::endl;
// ...
}
template <class T>
void foo(T && s) {
std::cout << "foo2" << std::endl;
func(s, ':');
//func(std::forward<T>(s), ':');
}
int main(int agrc, char *argv[])
{
//std::string s("a:b:c:d");
char s[8] = "abcdefg";
foo(std::move(s));
std::string s2("abcd")
foo(s2);
return 0;
}
If I replace func(s, ':')
using std::forward
, it makes no difference, the foo function will do the prefect forwarding without std::forward
in this case.
Is there mis-understanding of 'prefect forwarding' with std::forward
in C++11?