标签: list-initialization

是 char s[] = {"ABC"}; 合法的?

#include <stdio.h>
int main(){
    char s[] = {"hello"};
    printf("%s", s);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这段代码可以编译并成功执行,但是如何将一维数组(char[] s)赋值为二维数组({"hello"})呢?

c arrays declaration string-literals list-initialization

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

使用unique_ptr在函数调用中使用大括号初始化自动类型推导失败

鉴于下面的代码,unique_ptr和Bar之间的区别是什么,bar ( { new int } );但没有foo( { new int } );

#include <memory>

struct Bar {
    Bar() = default;
    Bar( int* m ) : m_( m ) {};
    ~Bar() { if ( m_ ) delete m_; }
    explicit operator bool() const { return m_; }
private:
    int* m_ {};
};

bool bar( Bar && a ) { return bool(a); }
bool foo( std::unique_ptr<int> && a) { return bool(a); }

int main() {
    bar( { } ); …
Run Code Online (Sandbox Code Playgroud)

c++ unique-ptr c++11 list-initialization

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

是否使用变量编译器特定的新结构{}的列表初始化?

使用Visual Studio 2013和C++编译器编写另一个链接列表并通过反复试验发生.这是特定于Visual C++还是标准的一部分?

我真的很喜欢这种语法.它非常干净.如果你已经写过一个链表lib,你知道你可以追逐指针,直到你的大脑被打结.这种语法虽然很清楚.

很多这样的东西都有静态初始化值,但是使用函数args作为初始化器?还没有看到.

知道GCC C/C++编译器是否产生预期结果将是令人欣慰的.任何人?

typedef struct link_in_list {
    struct link_in_list *next;
    int32_t key;
    int32_t value;
}   LINK, *pLINK;

// ----------------------------------------------------------------------------
pLINK prepend_list(pLINK head, int32_t key, int32_t value)
{
    if (NULL == head)   {
        // initialize with a constant, variable, and expression
        return new LINK{ NULL, key, (key * key) };
    }   else    {
        // initialize with stack variables
        return new LINK{ head, key, value };
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ c++11 list-initialization

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

对向量的列表初始化

注意:请不要仅仅因为根本原因是逗号运算符而关闭问题。这个问题的价值在于让社区了解以下方面的失败:

std::vector<std::pair<int, int>> vpairs_third{(0, 1), (0, -1)};
Run Code Online (Sandbox Code Playgroud)

是由于逗号运算符造成的。

我正在列表初始化一个包含整数对的向量。

我按照预期理解以下作品:

std::vector<std::pair<int, int>> vpairs_first{std::make_pair(0, 1), std::make_pair(0, -1)};
Run Code Online (Sandbox Code Playgroud)
std::vector<std::pair<int, int>> vpairs_second{{0, 1}, {0, -1}};
Run Code Online (Sandbox Code Playgroud)

但是,有人会解释为什么以下不起作用吗?

std::vector<std::pair<int, int>> vpairs_third{(0, 1), (0, -1)};
Run Code Online (Sandbox Code Playgroud)

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

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

C++ 11统一初始化(即列表初始化)不使用继承进行编译

struct B { 
  int b; 
  B(int i = 0) : b(i) {};  // constructor
};

struct D : B  {
  int d;
};

int main () {
  D obj = {1};  // <-- error
  // D obj {1}; // <-- error (different)

}
Run Code Online (Sandbox Code Playgroud)

上面的代码没有编译,并给出:

error: could not convert ‘{1}’ from ‘<brace-enclosed initializer list>’ to ‘D’
Run Code Online (Sandbox Code Playgroud)

即使我删除了"构造函数"行也是如此.如果我删除=符号,即D obj {1};它然后给出如下:

error: no matching function for call to ‘D::D(<brace-enclosed initializer list>)’
Run Code Online (Sandbox Code Playgroud)

解决此类问题的正确语法是什么?

c++ inheritance compiler-errors c++11 list-initialization

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

为什么我不能使用'0x80000000'来初始化int数组?

#include<iostream>
int main()
{
    int arr[1] = {0x80000000};
    std::cout<<arr[0]<<"\n";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码将收到以下错误:

错误:缩小'2147483648u'从'unsigned int'到'int'的转换{} [-Wnarrowing] int arr [1] = {0x80000000};

但以下代码完美运行:

#include<iostream>    
int main()
{
    int arr[1];
    arr[0] = 0x80000000;
    std::cout<<arr[0]<<"\n";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:为什么我不能使用'0x80000000'初始化一个数组int

c++ arrays int initialization list-initialization

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

子类的列表初始化

我想通过列表初始化来初始化作为 A 子类的类 B ( https://en.cppreference.com/w/cpp/language/list_initialization )

然而,它并不是这样工作的:

struct A {
   int x;
};

struct B : public A {
};

int main()
{
   A a{ 1 }; // compiles
   B b{ 2 }; // doesn't compile
}
Run Code Online (Sandbox Code Playgroud)

有没有办法通过列表初始化来初始化 B 的实例?

本质上,我想在不声明任何构造函数的情况下初始化 B 的实例。

c++ inheritance compiler-errors aggregate list-initialization

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