#include <array>
#include <vector>
#include <cinttypes>
#include <iostream>
using namespace std;
template<size_t N>
struct item_t {
array<uint32_t, N> weight = {0};
};
int main(void) {
vector<item_t<3>> items;
items.emplace_back({{9,2,3}});
cout << items[0].weight[0] << endl;
return 0;
};
Run Code Online (Sandbox Code Playgroud)
我在这里有点不知所措。错误在 emplace_back 线上,不知道如何解决。任何帮助或提示将不胜感激,谢谢。
编辑
gcc 版本 4.8.2
$ g++ -std=c++11 test.cpp
test.cpp: In function ‘int main()’:
test.cpp:16:30: error: no matching function for call to ‘std::vector<item_t<3ul> >::emplace_back(<brace-enclosed initializer list>)’
items.emplace_back({{9,2,3}});
^
test.cpp:16:30: note: candidate is:
In file included from /usr/include/c++/4.8/vector:69:0,
from test.cpp:2:
/usr/include/c++/4.8/bits/vector.tcc:91:7: note: void …Run Code Online (Sandbox Code Playgroud) 我有一个集合定义为 -
using Parameters = std::vector<int>;
using Group = std::pair<std::string, Parameters>;
std::vector<Group> inputs;
Run Code Online (Sandbox Code Playgroud)
我的意图是使用像这样的陈述
inputs.push_back(group0 /*What goes in here ?*/);
inputs.push_back(group1 /*What goes in here ?*/);
Run Code Online (Sandbox Code Playgroud)
如何初始化group0和group1使用初始化列表?像这样的代码似乎不起作用
inputs.push_back(std::make_pair("group0", {1, 2, 3, 4}));
Run Code Online (Sandbox Code Playgroud)
编辑:有是矢量,对初始化已经存在的问题,但我看不出任何地方second的std::pair又是一个集合.
请考虑这段代码:
#include <iostream>
int main()
{
struct A
{
int x;
int y;
int z;
int foo()
{
std::cout << "enter foo: " << this->x << "," << this->y << "," << this->z << std::endl;
return 5;
}
int moo()
{
std::cout << "enter moo: " << this->x << "," << this->y << "," << this->z << std::endl;
this->x = 1;
this->z = 10;
return 2;
}
};
A b { b.foo(), b.z = b.moo(), 3};
std::cout << "final: " …Run Code Online (Sandbox Code Playgroud) c++ struct object-lifetime language-lawyer list-initialization
从概念上讲,我认为以下内容并不侵犯隐私。但这是被禁止的。
struct A
{
int a;
int b;
int c;
};
struct B
{
int a;
int b;
private:
int c;
};
int main (int argc, char * argv[])
{
auto a = A{1,2,3}; //ok
auto b = A{1,2}; //ok
auto c = B{1,2,3}; //error
auto d = B{1,2}; //error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
添加手动构造函数将允许对私有成员进行大括号初始化。但聚合和 Pod 的优点在于您需要的编码量很少,因此这很烦人。
另一方面,我认为这是一种隐私侵犯,但这是标准允许的。
我对在C++ 11和C++ 17中如何/为什么调用构造函数感到困惑.
#include <iostream>
using namespace std;
//---
template<typename T>
struct StructTest
{
public:
const T Var = -1;
//---
// constexpr StructTest() = delete; // 1
// constexpr StructTest() = default; // 2
// constexpr StructTest(const StructTest &Source) = delete; // 3
// constexpr StructTest(const StructTest &Source) = default; // 4
// constexpr StructTest(const StructTest &Source) {cout << "Copy" << endl;}; // 5
};
//---
StructTest<int> A{};
StructTest<int> A1{1};
StructTest<int> A2(A);
//---
int main(void)
{
return(0);
};
Run Code Online (Sandbox Code Playgroud)
所以当我取消注释某些行的组合(并使用带有clang的c …
c++ defaulted-functions deleted-functions list-initialization
看下面的代码:
int arr[4];
for (int index = 0; index < 4; ++index) {
printf("%d\t", arr[index]);
}
Run Code Online (Sandbox Code Playgroud)
它打印随机值,如下所示:
27224 -6784 32766 0
Run Code Online (Sandbox Code Playgroud)
但是当我设置arr为 时{},它会打印零。
27224 -6784 32766 0
Run Code Online (Sandbox Code Playgroud)
0 0 0 0
Run Code Online (Sandbox Code Playgroud)
为什么?
typedef struct node {
int val;
int val2;
node(int a, int b) : val(a), val2(b) {}
node(int val) = delete;
}node;
int main()
{
node a = {3};
cout << a.val << " " << a.val2 << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码给出了编译错误,表明使用了已删除的函数node::node(int)。
但是,当我删除 时node(int a, int b) : a(val), b(val2) {},这段代码编译没有问题。这怎么会发生呢?
c++ initialization aggregate-initialization list-initialization c++20
我试图通过替换两个不同的双线程来处理Windows API时使代码变得不那么臃肿
TEMP t{0,1,2}; // let's say it's struct TEMP {int a; int b; int c}
SomeVeryVerboseFunctionName(&t);
Run Code Online (Sandbox Code Playgroud)
与单行
SomeVeryVerboseFunctionName(&TEMP{0,1,2});
Run Code Online (Sandbox Code Playgroud)
但偶然发现错误:
expression必须是左值或函数指示符.
经过多次尝试,我终于想出了编译的代码(MSVS 2013u4):
SomeVeryVerboseFunctionName(&(TEMP) TEMP{0,1,2});//explicit cast to the same type!
Run Code Online (Sandbox Code Playgroud)
为了更好地理解为什么需要演员,我设置了一个简单的测试项目:
#include <stdio.h>
struct A
{
int a;
int b;
A(int _a, int _b) : a(_a), b(_b) {};
};
struct B
{
int a;
int b;
};
template <typename T> void fn(T* in)
{
printf("a = %i, b = %i\n", in->a, in->b);
}
int main()
{
fn(&A{ 1, …Run Code Online (Sandbox Code Playgroud) 为什么下面的代码是非法的?
for (int index=0; index<3; index++)
{
cout << {123, 456, 789}[index];
}
Run Code Online (Sandbox Code Playgroud)
虽然这很好:
for (int value : {123, 456, 789})
{
cout << value;
}
Run Code Online (Sandbox Code Playgroud)
IDEOne中的代码:http://ideone.com/tElw1w
由于array没有构造函数或析构函数,也没有公共的非静态成员变量,因此array如何允许花括号初始化?不允许尝试初始化以下类型:
template<typename T, std::size_t num>
class Array
{
T data[num];
};
Run Code Online (Sandbox Code Playgroud)
我如何以这种方式初始化该类型,而无需任何构造函数或析构函数就可以初始化该类型,以保持该类型易于构造和破坏,并且不暴露私有数组成员?