使用XCode 3.2.3(64位),我得到以下奇怪的输出.我究竟做错了什么?
#include <iostream>
#include <typeinfo>
struct student {
};
int main()
{
int i;
student obj;
std::cout << typeid(i).name() << "\n";
std::cout << typeid(obj).name() << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
i
7student
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种获取类型名称的方法,类似于typeid引用.根据此页面,typeid删除参考.
如果type是引用类型,则结果引用引用的类型.
我正在寻找类似的代码
int x = 5;
int & y = x;
wcout << typeid( y ).name();
Run Code Online (Sandbox Code Playgroud)
但其输出是"int&"而不是"int".
从下面的程序的输出都将1A其A经历时c++filt -t.我可以看到返回类型在返回时被推导为值类型而不是rvalue引用类型std::move.对于大多数返回的用例来说,这是有意义的std::move,但这是什么原因?std::move返回一个右值引用,但为什么返回类型会自动推导为值类型?
#include <iostream>
#include <memory>
#include <utility>
#include <typeinfo>
struct A
: std::unique_ptr<int>
{
auto f()
{
return A();
}
auto g()
{
return std::move(A());
}
};
int main()
{
std::cout << typeid(decltype(A().f())).name() << ' ';
std::cout << typeid(decltype(A().g())).name() << ' ';
std::cout << typeid(A).name() << '\n';
}
Run Code Online (Sandbox Code Playgroud) 如果我想要bi是一个long int,不可能使用auto,因为它总是指定为int?
int q[10]={0};
cout << q << endl;
cout << &q << endl;
cout << &q[0] << endl;
Run Code Online (Sandbox Code Playgroud)
输出是
0x7fffd4d2f860
0x7fffd4d2f860
0x7fffd4d2f860
Run Code Online (Sandbox Code Playgroud)
现在当我这样做时->
int *r=q; // allowed
int *r=&q[0] // allowed
int *r=&q // not allowed
Run Code Online (Sandbox Code Playgroud)
为什么在本质上是同一件事时不允许第三次赋值?
我很困惑为什么range-for在我的例子中使用 ref ?
#include <vector>
#include <unordered_map>
using namespace std;
int main()
{
const unordered_map<char, string> d2c_map= { {'1', "abc"} };
const string digits{"1"};
vector<string> R;
for(const auto c : d2c_map.at(digits[0])) {
R.push_back(c); // <-------------------------???
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误表明 c 的类型是const char&:
error: no matching function for call to 'std::vector<std::__cxx11::basic_string<char> >::push_back(const char&)'
Run Code Online (Sandbox Code Playgroud)