标签: aggregate-initialization

用于初始化数组/结构的不熟悉的语法,寻找解释

我正在查看由名为OVP的公司(类似于qemu的产品)提供的"处理器建模指南".在其中,有一个类似于以下内容的小代码片段:

static or1kDispatchTableC dispatchTable = {
  // handle arithmetic instructions
  [OR1K_IT_ADDI] = disDefault,
  [OR1K_IT_ADDIC] = disDefault,
  [OR1K_IT_ANDI] = disDefault,
  [OR1K_IT_ORI] = disDefault,
  [OR1K_IT_XORI] = disDefault,
  [OR1K_IT_MULI] = disDefault
};
Run Code Online (Sandbox Code Playgroud)

我以前从未见过这样的语法. 删除了关于C++的无关紧要的东西

目前我没有能力下载/查看他们的东西,看看有什么定义,因此我的问题.如果你认识到这种语法,你能说出来吗?


编辑

or1kDispatchTableC是类型指针的typedef or1kDispatchTableCP,但我仍然没有任何东西or1kDispatchTableCP.

c syntax c99 aggregate-initialization c11

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

在C++ 11中私有地继承聚合类的类的聚合初始化

请考虑以下代码:

struct base
{
    int x, y, z;
};

struct derived : private base
{
    using base::base;
};

int main(int argc, const char *argv[])
{
    base b{1, 2, 3}; // Allowed
    derived d{1, 2, 3}; // Not allowed
}
Run Code Online (Sandbox Code Playgroud)

derived d{1, 2, 3};行使我的编译器(Clang 3.3)失败,错误"没有匹配的构造函数用于初始化'derived'".为什么是这样?有没有办法derived通过聚合初始化初始化?

c++ inheritance aggregate-initialization c++11

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

如何使用new运算符在表达式中聚合初始化STL容器?

我正在尝试做类似的事情:

using std::array;

array< array<int, 3>*, 10> arrsPtrs;

void f()
{
  arrsPtrs[0] = new array<int, 3> {1, 2, 3};         //bad!
  arrsPtrs[0] = new array<int, 3> {{1, 2, 3}};       //bad!
  arrsPtrs[0] = new array<int, 3> ( {1, 2, 3} );     //bad!
  arrsPtrs[0] = new array<int, 3> ( {{1, 2, 3}} );   //bad!
}
Run Code Online (Sandbox Code Playgroud)

是否可以在这种表达式中聚合初始化STL容器?

PS:可能很重要 - 我有VS2010.

c++ arrays stl new-operator aggregate-initialization

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

这是C++中的聚合初始化还是默认初始化?

考虑以下程序.

#include <iostream>
int main()
{
    int a=int{};
    std::cout<<a;
}
Run Code Online (Sandbox Code Playgroud)

它是使用聚合初始化还是默认初始化?我很迷惑.

c++ initialization aggregate-initialization

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

这个C++ C11语法是什么:"= {}"?

我在c ++文件中遇到了这种语法:

Obj obj = {};
Run Code Online (Sandbox Code Playgroud)

它有什么作用?它只是调用默认构造函数吗?

c++ aggregate-initialization uniform-initialization c++11

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

std :: array派生类聚合初始化

我正在制作一个源自的小助手类std::array.显然,构造函数不会继承,而是负责大括号初始化的构造函数; 例如:

template<typename T, size_t size>
struct foo : std::array<T,size>
{
     foo(int a, int b)
     : std::array<T,size>{a,b}
     {
          //nothing goes here since constructor is just a dummy that 
          //forwards all arguments to std::array constructor
     }
}

int main()
{
     foo<int,2> myobj = {1,2}; //brace initialization calls custom constructor with inner elements as arguments
}
Run Code Online (Sandbox Code Playgroud)

参数的数量必须完全匹配,所以我倾向于在构造函数中使用类似variadic函数的参数(因为我不仅每次都在数组中使用2个元素).使用这个,我如何将可变参数包转发给std::array构造函数?我对其他支持初始化方法持开放态度,允许转发给std::array构造函数.

注意:std::initializer_list需要运行时初始化,我正在寻找编译时/ constexpr兼容方法.谢谢.

c++ templates aggregate-initialization variadic-templates c++11

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

兼具聚合初始化和模板推导

背景:

\n

C++17 有两大特性:聚合初始化模板类型推导(对于类)。聚合初始化允许您实例化字段而无需复制或移动它们,并且模板类型推导使您不必指定参数的类型。

\n

下面代码中的类Wrapper就是一个例子。只要HAVE_MOVE_AND_COPY未定义,它就具有聚合初始化,但模板类型推导不起作用。

\n

另一方面,如果定义HAVE_MOVE_AND_COPY ,则模板类型推导有效,但聚合初始化会中断。我怎样才能两者兼得?

\n
#include <cstdio>\n#include <utility>\n\ntemplate<class T> \nstruct Wrapper {\n    T value;\n   #ifdef HAVE_MOVE_AND_COPY\n    Wrapper(T const & val) : value{val} {}\n    Wrapper(T && val) : value{std::move(val)} {}\n   #endif\n    Wrapper(Wrapper const &) = default;\n    Wrapper(Wrapper &&) = default;\n    \n\n};\n\nstruct VocalClass {\n    VocalClass() { puts("VocalClass()"); }\n    VocalClass(VocalClass const&) { puts("VocalClass(VocalClass const &)"); }\n    VocalClass(VocalClass &&) { puts("VocalClass(VocalClass &&)"); }\n};\n\nint main() {\n    Wrapper<VocalClass> w { VocalClass() …
Run Code Online (Sandbox Code Playgroud)

c++ templates aggregate-initialization type-deduction c++17

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

emplace_back 内的聚合初始化

在下面的代码中,我尝试使用 emplace_back 进行聚合初始化。这里的问题是 emplace_back 接受构造函数参数并直接在向量中构造对象,我想知道是否在 a 上调​​用了复制构造函数A{1,2,3}。但是,为了使聚合初始化起作用,该结构不应具有用户定义的构造函数。你知道幕后发生了什么吗?

#include <iostream>
#include <vector>

struct A {
    int x, y, z;
    /*A(int x, int y, int z) : x(x), y(y), z(z) 
    {
        std::cout << "ctor called\n";
    }
    A(const A& other) : x(other.x), y(other.y), z(other.z) {
        std::cout << "copy ctor called\n";
    }*/
};

int main() {

    std::vector<A> vec;

    vec.emplace_back(A{1, 2, 3});

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ aggregate-initialization emplace

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

C++17 中的聚合初始化可以执行复制省略吗?

鉴于:

//C++17
#include <string>
struct Foo {
    int i;
    std::string str;
};

int main() {
    Foo foo{1, std::string("Hello, world!")};
}
Run Code Online (Sandbox Code Playgroud)

可以Foo::i直接Foo::str1std::string(...)初始化,而不是复制到其中,并解释为什么可以/不能使用 C++17 标准(可能是一些用于测试目的的代码)?

如果不能,需要多少份?

c++ aggregate-initialization copy-elision c++17

0
推荐指数
1
解决办法
292
查看次数

通过显式构造函数初始化数组

我正在编写一个类,该类具有带const char*参数的显式构造函数。为了这个问题的目的和目的,它看起来像这样:

struct Symbol
{
    Symbol()=default;
    explicit Symbol(const char*);
};
Run Code Online (Sandbox Code Playgroud)

现在,出于文档目的,我想编写一个示例来初始化一个数组(array / vector / list-我不在乎确切的类型),并且我需要该示例尽可能简洁明了。理想的情况是这样的:

Symbol symbols[] = { "a", "b", "c"};
Run Code Online (Sandbox Code Playgroud)

由于explicit关键字,因此无法编译,因此我不准备将构造函数设为隐式。

我如何使这项工作有效,重点在于使示例代码尽可能具有表现力?

编辑:在Caleth的一点帮助下,我寻求Bolov的解决方案:

struct Symbol
{
    Symbol();
    explicit Symbol(const char*);

    template <class... Args> 
    static std::array<Symbol, sizeof...(Args)> Array(Args... args)
    {
        return {Symbol{args}...}; 
    } 
};

int main()
{
    auto symbols = Symbol::Array("a", "b", "c");
}
Run Code Online (Sandbox Code Playgroud)

c++ explicit-constructor aggregate-initialization c++11

0
推荐指数
1
解决办法
444
查看次数

c ++ 11努力理解为什么我不能统一初始化结构,但是可以初始化类似类型的std :: pair

给出以下代码:

#include <functional>
#include <string>
#include <iostream>

class test
{
public:
    struct info
    {
        std::string name {""};
        std::function<bool()> func;
    };

    //info my_info { "test_name", [&]{return member_func();} }; // <------ ERROR HERE

    std::pair<std::string, std::function<bool()>> my_info_pair { "test_name", [&]{return member_func();} };

    bool member_func()
    {
        std::cout << "member_func\n";
        return true;
    };

};

int main() {
    std::cout << "start\n";
    test t;
    std::cout << t.my_info_pair.first << std::endl;
    t.my_info_pair.second();
    std::cout << "end\n";
}
Run Code Online (Sandbox Code Playgroud)

此代码有效。但是,如果我取消注释行的注释-它试图以info与std :: pair 初始化相同的方式初始化结构,则它将失败。我不知道为什么...

错误得到的是:

prog.cc:15:60: error: could not convert '{"test_name", …
Run Code Online (Sandbox Code Playgroud)

c++ aggregate-initialization uniform-initialization c++11

0
推荐指数
1
解决办法
96
查看次数