我正在尝试使用GCC 4.7.1编译以下-std=c++11
标志:
std::map<std::string, auto> myMap;
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个对象来包含大量各种类型的Json数据(int string,bool)以及子结构(list,map),所以我无法在编译时声明字段值的类型时间,所以我以为我会使用auto
关键字.
但是,当我尝试编译它时,我得到以下内容
error: invalid use of ‘auto’
error: template argument 2 is invalid
error: template argument 4 is invalid
error: unable to deduce ‘auto’ from ‘<expression error>’
Run Code Online (Sandbox Code Playgroud)
有没有一种特殊的方法可以auto
用作模板参数,还是只是不可能?
我用方法创建了一个类,它返回对成员的引用(封装不好)。我正在使用自动重新调整功能
class classA
{
public:
classA(classA & rhs)
{
cout<<"copy constr A"<<endl;
};
classA() = default;
};
class classB
{
private:
classA obA;
public:
classA& getRefA(){return obA;}
};
int main()
{
classB obB;
auto ob = obB.getRefA();
}
Run Code Online (Sandbox Code Playgroud)
结果是复制构造A
我知道 auto 不会检测函数的引用。自动检测是否只检测类型而不参考?
如果我有这条线
auto* f = new boost::posix_time::time_input_facet("%d %b %Y %H:%M:%S");
Run Code Online (Sandbox Code Playgroud)
我必须删除指针吗?或者它自己清理?
delete f; // ??
Run Code Online (Sandbox Code Playgroud)
我知道auto_ptr和shared_ptr会自动删除,但我不知道auto
我在g ++ 7.3.1 -std = c ++ 14下面有一个简单的片段
#include<functional>
#include<iostream>
using namespace std;
struct S{
int m_i = 2;
auto& get2(){return ref(m_i);}
};
int main(){
S s;
s.get2()=4;
cout<<s.m_i<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它编译错误如下:
error: cannot bind non-const lvalue reference of type ‘std::reference_wrapper<int>&’ to an rvalue of type ‘std::reference_wrapper<int>’
auto& get2(){return ref(m_i);}
~~~^~~~~
In function ‘int main()’:
error: use of deleted function ‘std::reference_wrapper<_Tp>::reference_wrapper(_Tp&&) [with _Tp = int]’
s.get2()=4;
^
In file included from /usr/include/c++/7/bits/std_function.h:44:0,
from /usr/include/c++/7/functional:58,
from xxx.cpp:1:
/usr/include/c++/7/bits/refwrap.h:338:7: note: declared …
Run Code Online (Sandbox Code Playgroud) 我最近发现修改自动迭代向量中的数据不会为我带来正确的结果。例如,当我尝试对vector的vector元素进行排序时,某些元素未排序,但代码成功运行
vector<vector<int> > arr;
arr.push_back({38, 27});
for(auto v : arr)
{
sort(v.begin(), v.end());
}
Run Code Online (Sandbox Code Playgroud)
上面的代码在排序后的输出仍然是38、27。而当我以sort(arr [0] .begin(),arr [0] .end())进行排序时,结果是正确的。我用gcc编译。
我想知道如何为 Android Auto 创建自定义应用程序。现在,我知道我可以创建“媒体”和“通知”Android Auto 应用程序,但我想要一个自定义应用程序(带有自定义服务),因为我想创建一个谷歌助手流程并修改地图。
谷歌助手新流程
我想通过语音获得对某些请求的自定义响应,因为我想通过 Google 助理进行预约。我刚刚看到了一个 Android Auto 通知应用程序。使用这些方法,我可以发送和重播一些消息,但这不是新的或自定义的 Google 助理对话框。我怎么能用新的谷歌助手语音流创建一个新的应用程序。
请,如果有人有一个自定义的 Android Auto 应用程序示例来学习它,我将不胜感激!:D
地图
此外,我不知道我是否可以编辑谷歌地图以从我的 Android 应用程序中注释地图中的一些自定义地点,或者是否有必要在我的应用程序中创建我自己的地图。我不确定当我询问 Google 助理时,它是否适用于我的自定义 Google 地图,或者它是否会向我显示标准的 Google 地图(没有我的非凡地点)。
我需要创建这样的东西 —> ? 在此处输入图片说明
在此先感谢您的帮助!
google-maps custom-application auto android-auto google-assistant-sdk
这是一个简单的代码片段,用于给出自动日期类型的名称。所以我想知道 bitset 会有什么标识符。它返回:“St6bitsetILm32EE”。好的,bitset 是数据类型,32 是大小,我不确定这个名字还告诉我什么。我不知道 St6、I、LM 或 EE 指的是什么。澄清会很好。
// C++ program to demonstrate working of auto
// and type inference
#include <bits/stdc++.h>
using namespace std;
#define M 32
int main()
{
auto x = 5; //i for integer
auto y = 3.37; //D for double
auto ptr = &x; //Pi for pointer
auto z = "WTF";//PKc for string or char**
bitset <M> bset(2);
auto k = bset; //bitset :: St6bitsetILm32EE
cout << typeid(x).name() << endl
<< typeid(y).name() << endl …
Run Code Online (Sandbox Code Playgroud) 我知道 C++11 初始化vector
using 的方式auto
,实际上std::initializer_list
是初始化而不是vector
. 但是,给出以下代码:
#include <iostream>
#include <vector>
using namespace std;
int main() {
auto x = {1, 2};
cout << typeid(x).name() << endl;
auto z = (1, 2);
cout << z << ", type: " << typeid(z).name() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不明白:
x
返回St16initializer_listIiE
的类型是 'z' 返回的类型是 'i',使用 gcc-10 编译器。我们不应该只返回std::initializer_list
和'int'吗?z
:warning: left operand of comma operator has no effect [-Wunused-value]
。那么结果的第二部分是:2, …
为什么用auto
关键字定义变量不带有constexpr
用于初始化它的表达式的“性质”?
例如,请考虑以下代码:
#include <string_view>
constexpr std::string_view f() { return "hello"; }
static constexpr std::string_view g() {
constexpr auto x = f(); // (*)
return x.substr(1, 3);
}
int foo() { return g().length(); }
Run Code Online (Sandbox Code Playgroud)
使用 GCC 10.2 和--std=c++20 -fsanitize=undefined -O3
,编译为:
foo():
mov eax, 3
ret
Run Code Online (Sandbox Code Playgroud)
但是,如果我们删除 (*) 行上的 constexpr,我们将得到一个 27 行的程序,其中包含一堆指针、一个长字符串常量等。
笔记:
auto
wrt constexpr
ness的一般行为。该示例只是表明 GCC 不会将 x 视为constexpr
我们没有明确告诉它。我尝试分配 'a' 变量如下:
for (auto& a : getMap()[1])
Run Code Online (Sandbox Code Playgroud)
将垃圾值分配给a
. 但是如果我在如下所示首先声明变量后使用它,它可以正常工作。
auto vv = getMap()[1];
for (auto& a : vv)
Run Code Online (Sandbox Code Playgroud)
如果我在没有声明变量的情况下立即使用它,为什么会出现问题?
#include <string>
#include <map>
#include <memory>
#include <vector>
using namespace std;
typedef struct _mystruct {
} mystruct;
map<int, vector<shared_ptr<mystruct>>> mymap;
void init() {
vector<shared_ptr<mystruct>> v;
v.push_back(make_shared<mystruct>(mystruct()));
mymap[1] = v;
}
map<int, vector<shared_ptr<mystruct>>> getMap() {
return mymap;
}
int main()
{
init();
vector<shared_ptr<mystruct>> v2;
for (auto& a : getMap()[1]) {
v2.push_back(a);
}
auto vv = getMap()[1];
for (auto& a …
Run Code Online (Sandbox Code Playgroud)