C++ – 关于玄学左右值的示例

template <typename T> void LOrR(T&) { std::cout << "L" << std::endl; }
template <typename T> void LOrR(T&&) { std::cout << "R" << std::endl; }

template <typename T>
void FuncA(T& Temp)
{
    std::cout << typeid(T).name() << std::endl;
    std::cout << TIsLValueReference<T>::Value << std::endl;
    std::cout << TIsRValueReference<T>::Value << std::endl;
    LOrR(Temp);
    LOrR(Forward<T>(Temp));
}

template <typename T>
void FuncB(T&& Temp)
{
    std::cout << typeid(T).name() << std::endl;
    std::cout << TIsLValueReference<T>::Value << std::endl;
    std::cout << TIsRValueReference<T>::Value << std::endl;
    LOrR(Temp);
    LOrR(Forward<T>(Temp));
}

int main()
{
    int32 TempA = 33;
    FuncA(TempA);
    FuncB(TempA);
}
int
false
false
L
R
int
true
false
L
L

发表回复