C++ 中引用变量和常规变量的区别?
Difference between reference and regular variable in C++?
你在C++中定义引用后,引用和普通变量有什么区别吗?
比如我在下面的代码中定义了一个引用之后:
整数 x = 10;
int& xRef = x;
有什么方法可以判断 xRef 是对 int 的引用,而不仅仅是一个普通的 int? int& 是它自己的类型吗?
Is there any way to tell that xRef is a reference to an int, rather than just being a normal int?
您可以使用 std::is_reference:
#include <iostream>
#include <type_traits>
int main ()
{
int i = 0;
int& ri = i;
if (std::is_reference <decltype (i)>::value)
std::cout << "i is a reference\n";
if (std::is_reference <decltype (ri)>::value)
std::cout << "ri is a reference\n";
}
输出:ri is a reference
所以,
Is int& its own type?
是的。
你在C++中定义引用后,引用和普通变量有什么区别吗?
比如我在下面的代码中定义了一个引用之后: 整数 x = 10; int& xRef = x;
有什么方法可以判断 xRef 是对 int 的引用,而不仅仅是一个普通的 int? int& 是它自己的类型吗?
Is there any way to tell that xRef is a reference to an int, rather than just being a normal int?
您可以使用 std::is_reference:
#include <iostream>
#include <type_traits>
int main ()
{
int i = 0;
int& ri = i;
if (std::is_reference <decltype (i)>::value)
std::cout << "i is a reference\n";
if (std::is_reference <decltype (ri)>::value)
std::cout << "ri is a reference\n";
}
输出:ri is a reference
所以,
Is int& its own type?
是的。