这是这个问题的后续问题:声明constexpr initializer_list对象是否合法?.
从C++ 14开始,std::initializer_list该类的所有方法都标有constexpr.能够通过执行来初始化实例似乎很自然,
constexpr std::initializer_list<int> list = {1, 2, 3};
但是Clang 3.5抱怨list没有被常量表达式初始化.
正如dyp在评论中指出的那样,任何std::initializer_list文字类型的要求似乎都从规范中消失了.
如果我们甚至不能将类完全定义为constexpr,那么有什么意义呢?这是标准中的疏忽,将来会得到修复吗?
每个人都创建std::vector从std::initializer_list,但对于周围的其他方式?
例如.如果你使用a std::initializer_list作为参数:
void someThing(std::initializer_list<int> items)
{
...
}
Run Code Online (Sandbox Code Playgroud)
有时您将项目放在vector<T>文字列表中而不是文字列表中:
std::vector<int> v;
// populate v with values
someThing(v); // boom! No viable conversion etc.
Run Code Online (Sandbox Code Playgroud)
更一般的问题是:如何stl::initializer_list从STL可迭代创建,而不仅仅是std::vector.
我喜欢autoC++ 11.太棒了.但它有一个不一致,真的让我紧张,因为我一直绊倒它:
int i = 3; // i is an int with value 3
int i = int{3}; // i is an int with value 3
int i(3); // i is an int with value 3 (possibly narrowing, not in this case)
int i{3}; // i is an int with value 3
auto i = 3; // i is an int with value 3
auto i = int{3}; // i is an int with value 3
auto i(3); …Run Code Online (Sandbox Code Playgroud) 如果我有两个不同的常量成员变量,它们都需要基于相同的函数调用进行初始化,有没有办法在不调用函数两次的情况下做到这一点?
例如,分子和分母是常数的分数类。
int gcd(int a, int b); // Greatest Common Divisor
class Fraction {
public:
// Lets say we want to initialize to a reduced fraction
Fraction(int a, int b) : numerator(a/gcd(a,b)), denominator(b/gcd(a,b))
{
}
private:
const int numerator, denominator;
};
Run Code Online (Sandbox Code Playgroud)
这会浪费时间,因为 GCD 函数被调用了两次。您还可以定义一个新的类成员 ,gcd_a_b然后首先将 gcd 的输出分配给初始化列表中的那个,但这会导致浪费内存。
一般来说,有没有办法在不浪费函数调用或内存的情况下做到这一点?您可以在初始化列表中创建临时变量吗?
我正在快速使用C++ 0x,并使用g ++ 4.6进行测试
我只是尝试了下面的代码,认为它会工作,但它不会编译.我收到错误:
incompatible types in assignment of ‘std::initializer_list<const int>’ to ‘const int [2]’
struct Foo
{
int const data[2];
Foo(std::initializer_list<int const>& ini)
: data(ini)
{}
};
Foo f = {1,3};
Run Code Online (Sandbox Code Playgroud) 我有类Phenotype与以下构造函数:
Phenotype(uint8 init[NUM_ITEMS]);
Run Code Online (Sandbox Code Playgroud)
我可以像这样创建一个Phenotype:
uint8 data[] = {0,0,0,0,0};
Phenotype p(data);
Run Code Online (Sandbox Code Playgroud)
但是当我尝试创建这样的一个时,我收到一个错误:
Phenotype p = {0,0,0,0,0};
Run Code Online (Sandbox Code Playgroud)
输出:
$ make
g++ -Wall -g main.cpp -std=c++0x
main.cpp: In function ‘int main(int, char**)’:
main.cpp:109: error: no matching function for call to ‘Phenotype::Phenotype(<brace-enclosed initializer list>)’
main.cpp:37: note: candidates are: Phenotype::Phenotype(uint8*)
Run Code Online (Sandbox Code Playgroud)
该错误似乎表明有一种方法可以定义一个构造函数,该构造函数采用括号括起的初始化列表.有谁知道如何做到这一点?
我可以在初始化列表中使用10个相同的整数初始化STL向量吗?到目前为止,我的尝试都让我失望.
我不明白为什么初始化程序列表不能用于运算符的RHS.考虑:
class foo { };
struct bar
{
template<typename... T>
bar(T const&...) { }
};
foo& operator<<(foo& f, bar const&) { return f; }
int main()
{
foo baz;
baz << {1, -2, "foo", 4, 5};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
最新的Clang(gcc也)抱怨:
clang.cc:14:9: error: initializer list cannot be used on the right hand side of operator '<<'
baz << {1, -2, "foo", 4, 5};
^ ~~~~~~~~~~~~~~~~~~~~
^ ~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
为什么C++标准会禁止这个?或者换句话说,为什么这会失败而不是
baz << bar{1, -2, "foo", 4, 5};
Run Code Online (Sandbox Code Playgroud)
?
#include <initializer_list>
struct Obj {
int i;
};
Obj a, b;
int main() {
for(Obj& obj : {a, b}) {
obj.i = 123;
}
}
Run Code Online (Sandbox Code Playgroud)
该代码无法编译,因为来自的值initializer_list {a, b}被当作const Obj&,并且不能绑定到非const引用obj。
是否有一种简单的方法可以使类似的构造工作,即遍历像like a和bhere 这样的不同变量中的值。
下面是一个代码片段,可以在vs2015中编译和运行而不会出错
#include<iostream>
using namespace std;
class A {
public:
A(int b) :k(b) {}//second time
const int k = 666;//first time
};
int main() {
A a(555);
cout << a.k << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是555.但据我所知,const对象应该只初始化一次,之后值不可修改.