写这样的东西可以
typedef unsigned long DWORD;
DWORD nBytesRead = {};
Run Code Online (Sandbox Code Playgroud)
此变量在此表达式后是否包含0?
我遇到了这个 post可变参数模板函数来连接 std::vector 容器,建议使用以下语法:
template<typename T>
void append_to_vector(std::vector<T>& v1, const std::vector<T>& v2) {
std::cout << v2[0] << std::endl;
for (auto& e : v2) v1.push_back(e);
}
template<typename T, typename... A>
std::vector<T> concat_version3(std::vector<T> v1, const A&... vr) {
int unpack[] { (append_to_vector(v1, vr), 1)... };
(void(unpack));
return v1;
}
Run Code Online (Sandbox Code Playgroud)
我开始玩弄它以了解它是如何工作的,因为我还没有看到这个:
int unpack[] { (append_to_vector(v1, vr), 0)... };
(void(unpack));
Run Code Online (Sandbox Code Playgroud)
似乎这是某种动态生成的初始化列表,它也有副作用?我也对0上述无关紧要的事实感到困惑。我替换了 -1 和 5,这些值中的每一个也都工作得很好。
那么有人可以告诉我这种技术/语法的名称以及上面两行中究竟发生了什么?如果我错过了相关的 SO 帖子,我将非常感谢任何指点并道歉。
请参阅以下代码:
std::vector<int> v1{1, 2, 3};
std::vector<int> v2 = {1, 2, 3};
Run Code Online (Sandbox Code Playgroud)
我的问题是:
这两者有区别吗?我知道第一个必须是列表初始化,但第二个怎么样?
因为第二个有一个赋值符号,所以我认为编译器会首先使用它std::initializer_list来创建一个临时符号vector,然后使用复制构造函数将temp复制vector到v2.这是事实吗?
c++ initialization initializer-list c++11 list-initialization
在以下 C++ 代码中,为什么与赋值运算符一起使用的结构初始值设定项不会生成编译器错误?
这是编译器错误吗?
它适用于每个编译器吗?
它实际上是哪种作业?
class vector2D
{
public :
double x,y;
};
int main()
{
vector2D v1;
v1 = {1,2} ; // why does this compile and work?
}
Run Code Online (Sandbox Code Playgroud) struct SomeStruct {
int a;
int b;
int c;
int d;
};
void main() {
std::array<int, 4> arr {1, 2, 3, 4};
SomeStruct someStruct { ??? }; // <------ Initialize with arr? (Ideal would be {arr})
}
Run Code Online (Sandbox Code Playgroud)
如代码示例中所示,是否有一些快捷方式可以使用 arr 初始化 someStruct?而不是诉诸于:
SomeStruct someStruct {arr[0], arr[1], arr[2], arr[3]};
Run Code Online (Sandbox Code Playgroud) 我读到列表初始化通常优于使用圆括号的“旧”方式,因为它们不允许缩小转换。该auto关键字具有相似的属性,因为它根本避免了转换。我想知道以下两个语句是否等效:
auto a = SomeClass(arg_a, arg_b);
auto b = SomeClass{arg_a, arg_b};
Run Code Online (Sandbox Code Playgroud)
也就是说,除了 b 与 a 的名称不同这一事实之外;)
通过等效,我的意思是,对于任何参数SomeClass,在任何情况下,我都可以将导致a构造的表达式替换为导致b构造的构造,当然前提是我使用了auto关键字。是这种情况,还是有任何陷阱/注意事项?
使用 gcc 9.4.0 和 Eigen 3.3.7,以下编译没有问题:
\n#include <Eigen/Dense>\n\nint main(int Argc, char *Argv[]) {\n Eigen::Matrix<int, 1, 4> d = {1, 2, 3, 4};\n}\nRun Code Online (Sandbox Code Playgroud)\n将长度增加 1 会引发错误:
\n#include <Eigen/Dense>\n\nint main(int Argc, char *Argv[]) {\n Eigen::Matrix<int, 1, 5> d = {1, 2, 3, 4, 5};\n }\n\nfoo.cpp:6:48: error: could not convert \xe2\x80\x98{1, 2, 3, 4, 5}\xe2\x80\x99 from \xe2\x80\x98<brace-enclosed initializer list>\xe2\x80\x99 to \xe2\x80\x98Eigen::Matrix<int, 1, 5>\xe2\x80\x99\n 6 | Eigen::Matrix<int, 1, 5> d = {1, 2, 3, 4, 5};\n | ^\n | |\n | …Run Code Online (Sandbox Code Playgroud) 在C语言中,表达式
int *a = {};
Run Code Online (Sandbox Code Playgroud)
编译正常。
为什么我可以将 {} 分配给 int 指针。
{} 主要是什么,变量 a 中有什么。
在调试模式下,为什么变量a为NULL。
如何解释和理解这个表述呢?以及为什么它可以编译?
c pointers initialization language-lawyer list-initialization
我想用值初始化2 D向量,它给了我这个错误:
IntelliSense: initialization with '{...}' is not allowed for object of type "std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>>"
Run Code Online (Sandbox Code Playgroud)
使用以下内容时,我会得到以上错误?
vector<vector<int>> A =
{ { 0, 0, 0, 0, 0, 0 },
{ 0, 1, 2, 2, 4, 1 },
{ 0, 3, 4, 1, 5, 2 },
{ 0, 2, 3, 3, 2, 4 },
{ 0, 4, 1, 5, 4, 6 },
{ 0, 6, 3, 2, 1, 3 } };
Run Code Online (Sandbox Code Playgroud) 假设我有这个结构:
struct MyStruct {
int field1;
char *field2;
MyStruct(int a, char* b): field2(b) {
field1 = doStuff(a);
}
MyStruct(int a): MyStruct(a, nullptr) {}
~MyStruct();
}
Run Code Online (Sandbox Code Playgroud)
据我所知,这不是一个聚合,因为我有一些构造函数.
我想要实现的是以自定义方式使用花括号初始化程序,这意味着使用如下代码:
MyStruct x = { 1, "string" };
Run Code Online (Sandbox Code Playgroud)
它隐式调用正确的构造函数(在本例中是第一个).
这有可能吗?