Skip to content

Instantly share code, notes, and snippets.

@LihuaWu
Created January 13, 2015 02:08
Show Gist options
  • Save LihuaWu/c3696a21a8ad64176a54 to your computer and use it in GitHub Desktop.
Save LihuaWu/c3696a21a8ad64176a54 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <functional>
#include <iostream>
using namespace std;
namespace UseConstAndNonConstLvalueRef {
void F(int&& a) {
printf("rvalue ref: %d\n", a);
}
//void F(int& a) {
// printf("lvalue ref: %d\n", a);
//}
void F(int a) {
printf("value: %d\n", a);
}
//G can accept parameter types like non-const-lvalue, const-lvalue, non-const-rvalue, const-rvalue
template <typename T>
void G(const T& a) {
F(a);
}
template <typename T>
void G(T& a) {
F(a);
}
}
int main() {
int a = 6;
const int b = 4;
UseConstAndNonConstLvalueRef::G(move(a));
UseConstAndNonConstLvalueRef::G(a);
return 0;
}
@LihuaWu
Copy link
Author

LihuaWu commented Jan 13, 2015

运行的结果是两个函数最终调用的都是F(int) 这个函数。这是为什么呢? 我注意到37行是调用了G(const T&)这个函数,传参结束后,发生了什么事情?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment