我有以下示例代码。是否可以在不指定对象向量中的“测试”的情况下初始化对象列表,或者这是最好的方法?谢谢。
class Test {
public:
Test(const std::initializer_list<int> list) : m_(list) {
}
private:
std::vector<int> m_;
};
int main(int argc, char **argv) {
std::vector<Test> v = { Test({1, 2, 3}), Test({1, 2, 4}) };
}
Run Code Online (Sandbox Code Playgroud) 我正在为结构使用初始值设定项列表。但是,它不适用于继承。
这段代码很好。
struct K {
int x, y;
};
K k {1, 2};
Run Code Online (Sandbox Code Playgroud)
但是,这会产生错误。
struct L : public K {};
L l {1, 2};
Run Code Online (Sandbox Code Playgroud)
此外,这不起作用。
struct X {
int x;
};
struct Y : public X {
int y;
};
Y y {1, 2};
Run Code Online (Sandbox Code Playgroud)
有没有办法使用具有继承结构的初始化列表。我在模板中使用它们,因此无论它是否是继承类,它都不会编译。
//parameter pack sum example
constexpr int sum(int N= 0)
{
return N;
}
template<typename ...Args>
constexpr int sum(int first, int second, Args ...N)
{
return first + second + sum(N...);
}
int main()
{
std::cout << sum<int>(1,6,3);
}
Run Code Online (Sandbox Code Playgroud)
是否有可能在编译时通过std::initializer_list<int>我如何递归迭代来计算这个总和。
考虑这个程序-
#include <string>
#include <vector>
#include <set>
void fun(const std::string& val) {
}
void fun(std::vector<std::string> val) {
}
int main()
{
std::set<std::string> example;
fun({std::begin(example), std::end(example)});
}
Run Code Online (Sandbox Code Playgroud)
在编译时,我遇到了这些错误-
prog.cc: In function 'int main()':
prog.cc:13:49: error: call of overloaded 'fun(<brace-enclosed initializer list>)' is ambiguous
13 | fun({std::begin(example), std::end(example)});
| ^
prog.cc:4:6: note: candidate: 'void fun(const string&)'
4 | void fun(const std::string& val) {
| ^~~
prog.cc:7:6: note: candidate: 'void fun(std::vector<std::__cxx11::basic_string<char> >)'
7 | void fun(std::vector<std::string> val) {
| ^~~
Run Code Online (Sandbox Code Playgroud)
我知道它std::string有一个构造函数重载,它接受 …
以下代码在以 Release 模式编译的 Visual Studio 2019 上失败。
#include <iostream>
#include <iterator>
#include <initializer_list>
int main( int, char** )
{
std::initializer_list<int> v = {};
std::initializer_list<int> i = { 1, 2, 3 };
v = i;
std::copy( v.begin(), v.end(), std::ostream_iterator<int>( std::cout, " " ) );
std::cout << std::endl;
v = { 1, 2, 3 };
std::copy( v.begin(), v.end(), std::ostream_iterator<int>( std::cout, " " ) );
std::cout << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
v 的第二次初始化似乎失败了,输出如下:
1 2 3
17824704 10753212 0
Run Code Online (Sandbox Code Playgroud)
但是在调试模式下构建或使用其他编译器(gcc、clang)构建时。输出如预期:
1 2 3
1 2 …Run Code Online (Sandbox Code Playgroud) 这是问题所在:
int main()
{
constexpr std::initializer_list<int> my_ints {1, 2, 3};
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 g++ (x86_64-posix-seh-rev0, version 8.1.0) 编译上述内容。但是 VS Code 引发了以下警告:
“表达式必须有一个常量值——生命周期有限的临时引用或指针”
当我删除说明constexpr符时,也就是当我将代码修改为
int main()
{
std::initializer_list<int> my_ints {1, 2, 3};
}
Run Code Online (Sandbox Code Playgroud)
错误消失了,所以声明 a 似乎有问题constexpr initializer_list。
然而根据这个参考(https://en.cppreference.com/w/cpp/utility/initializer_list/initializer_list),声明一个constexpr initializer_list.
有人可以更深入地了解其工作原理constexpr并initializer_list解释为什么会发生这种情况吗?
c++ intellisense initializer-list constexpr visual-studio-code
为什么此代码无效?
auto f() {
if (true) return 0;
return {};
}
Run Code Online (Sandbox Code Playgroud)
解析后0,我认为gcc应该知道函数的返回类型f是int,但它仍然解释{}为initializer_list解析最终返回子句时,为什么?
如果我写这个
std::vector<std::string> v{"one","two","three"};
Run Code Online (Sandbox Code Playgroud)
推断到关联std::initializer_list模板的类型是什么?换句话说,当char *字符串文字转换为std::string?
声明它是一个更好的主意
std::vector<std::string> v{std::string("one"),
std::string("two"),
std::string("three")};
Run Code Online (Sandbox Code Playgroud)
避免与涉及的模板的类型推导机制相关的问题?我会对此保持相同的优化吗?
在C++中,我们可以在构造函数中使用赋值来初始化对象,或者我们可以使用初始化列表.对于const数据成员或作为对象的数据成员,初始化列表是唯一的方法.但是,当参数的名称与数据成员的名称匹配时,this-> name(name)不起作用.如果名称相同,有没有办法做到这一点?
class A {
private:
int _x,y;
public:
A(int x, int y) : _x(x), // this works
this->y(y) // this does not work
{}
};
Run Code Online (Sandbox Code Playgroud)
我想也许这是一个操作顺序问题所以我试过:
(这 - > Y)(y)的
但那也不对.
我有一个基础和派生类的应用程序.我需要在派生类中有一个基类的字段,但是在初始化它时会遇到一些问题.这是代码:
#include <iostream>
using namespace std;
class X
{
public :
X( int x ) { }
} ;
class Y : public X
{
X x ;
Y* y ;
Y( int a ) : x( a ) { }
} ;
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
而错误:
/tmp/test.cpp||In constructor ‘Y::Y(int)’:|
/tmp/test.cpp|14|error: no matching function for call to ‘X::X()’|
/tmp/test.cpp|14|note: candidates are:|
/tmp/test.cpp|7|note: X::X(int)|
/tmp/test.cpp|7|note: candidate expects 1 argument, 0 provided|
/tmp/test.cpp|4|note: X::X(const X&)|
/tmp/test.cpp|4|note: candidate expects 1 …Run Code Online (Sandbox Code Playgroud) c++ ×10
initializer-list ×10
c++11 ×3
inheritance ×2
templates ×2
c++20 ×1
constexpr ×1
intellisense ×1
iterator ×1
overloading ×1
return ×1
stdvector ×1
struct ×1