#include <stdio.h>
int main(){
char s[] = {"hello"};
printf("%s", s);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这段代码可以编译并成功执行,但是如何将一维数组(char[] s)赋值为二维数组({"hello"})呢?
鉴于下面的代码,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) 使用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) 注意:请不要仅仅因为根本原因是逗号运算符而关闭问题。这个问题的价值在于让社区了解以下方面的失败:
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) 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)
解决此类问题的正确语法是什么?
#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?
我想通过列表初始化来初始化作为 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
c++ ×6
c++11 ×4
arrays ×2
inheritance ×2
aggregate ×1
c ×1
declaration ×1
int ×1
std-pair ×1
stdvector ×1
unique-ptr ×1