标签: initializer-list

为什么std :: initializer_list不是内置的语言?

为什么不std::initializer_list内置核心语言?

在我看来,它是C++ 11的一个非常重要的特性,但它没有自己的保留关键字(或类似的东西).

相反,initializer_list只是来自标准库的模板类,它具有来自编译器处理的新braced-init-list语法的特殊隐式映射. {...}

起初认为,这个解决方案非常黑客.

这是现在实现C++语言新增功能的方式:通过某些模板类的隐式角色而不是核心语言?


请考虑以下示例:

   widget<int> w = {1,2,3}; //this is how we want to use a class
Run Code Online (Sandbox Code Playgroud)

为什么选择了新课程:

   widget( std::initializer_list<T> init )
Run Code Online (Sandbox Code Playgroud)

而不是使用类似于以下任何想法的东西:

   widget( T[] init, int length )  // (1)
   widget( T... init )             // (2)
   widget( std::vector<T> init )   // (3)
Run Code Online (Sandbox Code Playgroud)
  1. 一个经典阵列,你可以在const这里和那里添加
  2. 语言中已经存在三个点(var-args,现在是可变参数模板),为什么不重用语法(并让它感觉内置)
  3. 只是一个现有的容器,可以添加const&

所有这些都已经成为语言的一部分.我只写了我的第一个想法,我相信还有很多其他方法.

c++ std initializer-list c++11

93
推荐指数
3
解决办法
8765
查看次数

在构造函数初始值设定项中初始化成员数组

class C 
{
public:
 C() : arr({1,2,3}) //doesn't compile
{}
    /*
    C() : arr{1,2,3} //doesn't compile either
{}
    */
private:
 int arr[3];
};
Run Code Online (Sandbox Code Playgroud)

我相信原因是数组只能用=语法初始化,即:

int arr[3] = {1,3,4};
Run Code Online (Sandbox Code Playgroud)

问题

  1. 我该怎么做我想做的事情(也就是说,在构造函数中初始化一个数组(不在主体中分配元素)).它甚至可能吗?
  2. C++ 03标准是否对在ctor初始化器中初始化聚合(包括数组)有什么特别之处?或者上述代码的无效性是其他一些规则的必然结果?
  3. C++ 0x初始化列表是否解决了这个问题?

PS请不要提及矢量,boost ::数组,以及它们对数组的优越性,我很清楚.

c++ initializer-list ctor-initializer aggregate-initialization c++11

92
推荐指数
3
解决办法
14万
查看次数

何时使用支架封闭的初始化器?

在C++ 11中,我们有了用于初始化类的新语法,它为我们提供了如何初始化变量的大量可能性.

{ // Example 1
  int b(1);
  int a{1};
  int c = 1;
  int d = {1};
}
{ // Example 2
  std::complex<double> b(3,4);
  std::complex<double> a{3,4};
  std::complex<double> c = {3,4};
  auto d = std::complex<double>(3,4);
  auto e = std::complex<double>{3,4};
}
{ // Example 3
  std::string a(3,'x');
  std::string b{3,'x'}; // oops
}
{ // Example 4
  std::function<int(int,int)> a(std::plus<int>());
  std::function<int(int,int)> b{std::plus<int>()};
}
{ // Example 5
  std::unique_ptr<int> a(new int(5));
  std::unique_ptr<int> b{new int(5)};
}
{ // Example 6
  std::locale::global(std::locale("")); // copied from 22.4.8.3 …
Run Code Online (Sandbox Code Playgroud)

c++ initializer-list c++11

91
推荐指数
3
解决办法
3万
查看次数

我可以列出 - 初始化只移动类型的向量吗?

如果我通过我的GCC 4.7快照传递以下代码,它会尝试将unique_ptrs 复制到向量中.

#include <vector>
#include <memory>

int main() {
    using move_only = std::unique_ptr<int>;
    std::vector<move_only> v { move_only(), move_only(), move_only() };
}
Run Code Online (Sandbox Code Playgroud)

显然,因为std::unique_ptr不可复制而无法工作:

错误:使用已删除的函数'std :: unique_ptr <_Tp,_Dp> :: unique_ptr(const std :: unique_ptr <_Tp,_Dp>&)[with _Tp = int; _Dp = std :: default_delete; std :: unique_ptr <_Tp,_Dp> = std :: unique_ptr]'

GCC是否正确尝试从初始化列表中复制指针?

c++ initializer-list move-semantics c++11

89
推荐指数
4
解决办法
1万
查看次数

initializer_list和移动语义

我允许将元素移出std::initializer_list<T>

#include <initializer_list>
#include <utility>

template<typename T>
void foo(std::initializer_list<T> list)
{
    for (auto it = list.begin(); it != list.end(); ++it)
    {
        bar(std::move(*it));   // kosher?
    }
}
Run Code Online (Sandbox Code Playgroud)

由于std::intializer_list<T>需要特殊的编译器注意并且没有像C++标准库的普通容器那样的值语义,所以我宁愿安全而不是抱歉并且问.

c++ templates initializer-list move-semantics c++11

88
推荐指数
2
解决办法
1万
查看次数

C编译错误:"可能无法初始化可变大小的对象"

为什么我使用以下代码收到错误"可能无法初始化可变大小的对象"?

int boardAux[length][length] = {{0}};
Run Code Online (Sandbox Code Playgroud)

c compiler-errors initializer-list variable-length-array

83
推荐指数
5
解决办法
14万
查看次数

std :: map扩展初始化列表会是什么样的?

如果它甚至存在,std::map扩展的初始化列表会是什么样的?

我已经尝试了一些组合......好吧,我能用GCC 4.4想到的一切,但没有发现任何编译.

c++ dictionary initializer-list c++11

81
推荐指数
2
解决办法
5万
查看次数

C++ 11初始化列表失败 - 但仅限于长度为2的列表

我找到了一个不起眼的日志记录错误,事实上长度为2的初始化列表似乎是一个特例!这怎么可能?

代码是使用Apple LLVM版本5.1(clang-503.0.40)编译的CXXFLAGS=-std=c++11 -stdlib=libc++.

#include <stdio.h>

#include <string>
#include <vector>

using namespace std;

typedef vector<string> Strings;

void print(string const& s) {
    printf(s.c_str());
    printf("\n");
}

void print(Strings const& ss, string const& name) {
    print("Test " + name);
    print("Number of strings: " + to_string(ss.size()));
    for (auto& s: ss) {
        auto t = "length = " + to_string(s.size()) + ": " + s;
        print(t);
    }
    print("\n");
}

void test() {
    Strings a{{"hello"}};                  print(a, "a");
    Strings b{{"hello", "there"}};         print(b, "b");
    Strings c{{"hello", …
Run Code Online (Sandbox Code Playgroud)

c++ initializer-list c++11

71
推荐指数
2
解决办法
2933
查看次数

为什么在C++ 14中对数组的初始化仍然需要双括号?

使用C++ 14标准,std::array可以使用单个括号进行初始化(请参阅http://en.cppreference.com/w/cpp/container/array):

然而,这并没有为工作std::arraystd::pair.

为什么这些工作:

std::pair<int, int> p { 1, 2 };
std::array<int, 3> a {1, 2, 3};
Run Code Online (Sandbox Code Playgroud)

但是这工作:

std::array<std::pair<int, int>, 3> b {{1, 11}, {2, 22}, {3, 33}};
Run Code Online (Sandbox Code Playgroud)

虽然这确实有效吗?

std::array<std::pair<int, int>, 3> b {{{1, 11}, {2, 22}, {3, 33}}};
Run Code Online (Sandbox Code Playgroud)

此外,为了完成,一个好的旧数组的初始化确实适用于单个大括号

std::pair<int, int> c[3] {{1, 11}, {2, 22}, {3, 33}};
Run Code Online (Sandbox Code Playgroud)

c++ initializer-list c++14

62
推荐指数
3
解决办法
3929
查看次数

为什么初始化列表中的元素数会导致模糊的调用错误?

为什么doSomething编译器会先调用前两个,但是在列表中使用两个元素会导致调用不明确?

#include <vector>
#include <string>

void doSomething(const std::vector<std::string>& data) {}

void doSomething(const std::vector<int>& data) {}

int main(int argc, char *argv[])
{
    doSomething({"hello"}); // OK
    doSomething({"hello", "stack", "overflow"}); // OK
    doSomething({"hello", "stack"}); // C2668 'doSomething': ambiguous call

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

c++ initializer-list ambiguous overload-resolution

59
推荐指数
3
解决办法
2169
查看次数