标签: list-initialization

模板化结构的花括号括起来的初始化列表

#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)

c++ templates struct c++11 list-initialization

4
推荐指数
1
解决办法
1648
查看次数

矢量 - 对统一初始化

我有一个集合定义为 -

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)

如何初始化group0group1使用初始化列表?像这样的代码似乎不起作用

inputs.push_back(std::make_pair("group0", {1, 2, 3, 4}));
Run Code Online (Sandbox Code Playgroud)

编辑:有矢量,对初始化已经存在的问题,但我看不出任何地方secondstd::pair又是一个集合.

c++ stdvector c++11 std-pair list-initialization

4
推荐指数
1
解决办法
409
查看次数

这是未定义的行为还是struct init的错误?

请考虑这段代码:

#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

4
推荐指数
1
解决办法
176
查看次数

为什么具有私有成员的聚合不支持 C++ 大括号初始化?

从概念上讲,我认为以下内容并不侵犯隐私。但这是被禁止的。

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++ uniform-initialization list-initialization

4
推荐指数
1
解决办法
1542
查看次数

C++ 11删除/默认的构造函数

我对在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

4
推荐指数
2
解决办法
160
查看次数

C中“int arr[] = {}”和“int arr[]”的区别

看下面的代码:

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)

为什么?

c initialization list-initialization

4
推荐指数
2
解决办法
949
查看次数

C++ 初始化奇怪的行为

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

4
推荐指数
1
解决办法
150
查看次数

为什么通过地址传递大括号初始化临时需要在MSVS中显式转换为相同类型

我试图通过替换两个不同的双线程来处理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)

c++ casting visual-c++ c++11 list-initialization

3
推荐指数
2
解决办法
228
查看次数

尝试更好地理解C++大括号初始化语法

为什么下面的代码是非法的?

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

c++ initializer-list list-initialization

3
推荐指数
1
解决办法
91
查看次数

std :: array大括号如何初始化?

由于array没有构造函数或析构函数,也没有公共的非静态成员变量,因此array如何允许花括号初始化?不允许尝试初始化以下类型:

template<typename T, std::size_t num>
class Array
{
    T data[num];
};
Run Code Online (Sandbox Code Playgroud)

我如何以这种方式初始化该类型,而无需任何构造函数或析构函数就可以初始化该类型,以保持该类型易于构造和破坏,并且不暴露私有数组成员?

c++ arrays list-initialization

3
推荐指数
1
解决办法
68
查看次数