上下文:我认为使用以下编译的SSCCEcompile: g++ -std=c++11 main.cpp将对固定大小的数组进行零初始化arr:
void foo(int arr[4] = {0}) { }
int main(int argc, char const *argv[]) {
foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然而,我调试gdb并发现arr = 0x0(空)。我(疯狂地)猜测这是因为int arr[4]很像,int* arr但另外告诉编译器只允许长度为 4 的数组。因此,实际发生的情况是,int arr* = {0}即我只是使用初始化列表来对指针进行零初始化。
我知道我可以通过恢复使用重载而不是默认参数来规避这个问题:
void foo(int arr[4]) { }
void foo() {
int arr[4] = {0};
foo(arr);
}
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,这确实给了我一个零初始化的固定大小数组foo。然而,我认为通过使用一些默认参数将这两个重载函数合并为一个函数会很好,类似于 SSCCE。
问题:C++11 中默认参数固定大小数组的零初始化是否可行。如果是这样,怎么办?
我正在尝试使用引用包装器向量运行一个示例,但在向量变量声明处遇到编译错误。这是代码:
\n\n#include <iostream>\n#include <vector>\n#include <functional>\n\nusing namespace std;\n\nstruct Base\n{\n virtual void print() = 0;\n};\n\nstruct X1: public Base\n{\n void print() override\n {\n cout << "X1\\n";\n }\n};\n\nstruct X2: public Base\n{\n void print() override\n {\n cout << "X2\\n";\n }\n};\n\n\nint main()\n{\n X1 x1;\n X2 x2;\n vector<reference_wrapper<Base>> X{cref(x1), cref(x2)};\n}\nRun Code Online (Sandbox Code Playgroud)\n\n向量构造函数std::initializer_list存在。传递值的类型必须是const T,它std::cref会返回,那么为什么它会抱怨:
/home/u1/sandbox/c++/trash/untitled/main.cpp:33: error: no matching function for call to \xe2\x80\x98std::vector<std::reference_wrapper<Base> >::vector(<brace-enclosed initializer list>)\xe2\x80\x99\n vector<reference_wrapper<Base>> X{cref(x1), cref(x2)};\n ^\nRun Code Online (Sandbox Code Playgroud)\n\n?\n(如果重要的话,使用 gcc -std=c++17 构建)
\n我知道 C++11 初始化vectorusing 的方式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, …我知道下面的代码块编译
#include<initializer_list>
int main()
{
std::initializer_list<int> li = {1,2,3,4};
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这也会编译
#include<iostream>
int main()
{
std::initializer_list<int> li = {1,2,3,4};
}
Run Code Online (Sandbox Code Playgroud)
被<initializer_list>列入<iostream>?根据this,它似乎不是。这可能是机器/编译器相关的东西吗?
_numbers我执行了以下操作,作为允许通过以下方式对成员容器进行只读访问的廉价方法numbers:
class Foo {
Foo() : _numbers({}), numbers(_numbers) {
// some code that populates `numbers` via `numbers.push_back(...)`
}
private:
std::vector<int> _numbers;
public:
const std::vector<int>& numbers;
}
Run Code Online (Sandbox Code Playgroud)
然而,这样做我发现它numbers是空的,而在其他情况下它将包含与 相同的元素_numbers。
更准确地说,这似乎是未定义的行为。在我的真实示例(这是一个简化版本)中,我有多个采用此方案的引用容器对,其中填充的数据在某些对的常量引用成员中可见,而对于某些对则不可见。
知道这有什么问题吗?非常感谢任何帮助。
编辑这是一个最小的工作示例:
#include <vector>
struct Foo2 {
public:
const int max1;
const int center1;
Foo2(const int max1_);
private:
std::vector<int> _numbers1, _numbers2;
public:
const std::vector<int>& numbers1, numbers2;
};
Foo2::Foo2(const int max1_)
: max1(max1_), center1(max1_/2),
_numbers1({}), _numbers2({}),
numbers1(_numbers1),
numbers2(_numbers2)
{
cout << max1 …Run Code Online (Sandbox Code Playgroud) 我从向量继承了我的类,我希望能够像向量一样将列表分配给我的类。
我的代码如下:
#include <vector>
using namespace std;
template<typename T>
class Matrix
: public vector<vector<T>>
{
public:
Matrix( vector<vector<T>> && m )
: vector<vector<T>>( m )
{}
// Tried this approach, but it doesn't work
// Matrix(std::initializer_list<std::initializer_list<T>> l){
// }
}
int main()
{
Matrix<int> m({{0, 1}, {2, 3}}); // it works
// Matrix<int> m = {{0, 1}, {2, 3}}; // error: no instance of constructor "Matrix<T>::Matrix [with T=int]" matches the argument list -- argument types are: ({...}, {...})
}
Run Code Online (Sandbox Code Playgroud) 我正在使用下面的类创建一个List对象:
public class Item
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public string D { get; set; }
public int E { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
像这样:
public List<Item> Flower = new List<Item>{};
Run Code Online (Sandbox Code Playgroud)
我如何使用结构如此的csv文件的内容初始化此列表?
A,B,C,D,1
Run Code Online (Sandbox Code Playgroud) 当我在下面编写一些代码时,我感到很奇怪.我期待相同的输出,但结果证明是错误的.为什么2个语句具有不同的输出,(expression-list)和{initializer-list}之间有什么区别?
cout << string(4, 'c') << endl;
cout << string{ 4, 'c' } << endl;
Run Code Online (Sandbox Code Playgroud)
输出是:
cccc
c //a square '' before 'c'
Run Code Online (Sandbox Code Playgroud) 打印数组时,初始化整数有效.
int a[MAX_SIZE] = {1,2,3,4,5,6,7,8,9,10};
for (int i = 0; i <= (MAX_SIZE - 1); i++)
{
printf("%3d",a[i]);
}
Run Code Online (Sandbox Code Playgroud)
但是,我想知道为什么初始化一个指向整数("walker")的指针不起作用:
int a[MAX_SIZE] = {1,2,3,4,5,6,7,8,9,10};
for (int *aWalk = a, int *aEnd = a + MAX_SIZE - 1; aWalk <= aEnd; aWalk++)
{
printf("%3d", *aWalk);
}
Run Code Online (Sandbox Code Playgroud)