Created
January 13, 2015 02:08
-
-
Save LihuaWu/c3696a21a8ad64176a54 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
运行的结果是两个函数最终调用的都是F(int) 这个函数。这是为什么呢? 我注意到37行是调用了G(const T&)这个函数,传参结束后,发生了什么事情?