从初始化器列表初始化,但没有{{{{{{{{...}}}}}}}}? [英] Initializing from an initializer list, but without {{{{{{{{ ... }}}}}}}}?

查看:160
本文介绍了从初始化器列表初始化,但没有{{{{{{{{...}}}}}}}}?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近绊倒了初始化列表的一些问题。考虑一个存储地图类数据的程序

I recently stumbles across some problem with initializer lists. Consider a program that stores map-like data

struct MyMapLike {
  MyMapLike(std::map<std::string, int> data)
    :data(std::move(data))
  { }

private:
  std::map<std::string, int> data;
};

这看起来很简单。但是当初始化它时,它变得丑陋。我想让它看起来像

That looks straight forward. But when initializing it, it becomes ugly. I want to let it look like

MyMapLike maps = { { "One", 1 }, { "Two", 2 } };

但是编译器不想接受这个,因为上面的意思是它应该查找可分别接受 {One,1} {Two,2} 的双参数构造函数。我需要添加额外的大括号,使它看起来像一个单参数构造函数接受 {{...},{...}}

But the compiler doesn't want to accept this, because the above means that it should look for a two-parameter constructor that can accept { "One", 1 } and { "Two", 2 } respectively. I need to add extra braces, to make it look like a single-parameter constructor accepting the { { ... }, { ... } }

MyMapLike maps = { { { "One", 1 }, { "Two", 2 } } };

我不想这样写。由于我有一个类似地图的类,并且初始化器具有映射列表的抽象值,我想使用前一个版本,并且独立于任何这样的实现细节,例如构造函数的嵌套级别。

I would not like to write it like that. Since I have a map-like class, and the initializer has the abstract value of a mapping-list, I would like to use the former version, and be independent of any such implementation details like level of nesting of constructors.

一个解决方法是声明一个初始化列表构造函数。

One work around is to declare an initializer-list constructor

struct MyMapLike {
  MyMapLike(std::initializer_list< 
    std::map<std::string, int>::value_type
    > vals)
    :data(vals.begin(), vals.end())
  { }

  MyMapLike(std::map<std::string, int> data)
    :data(std::move(data))
  { }

private:
  std::map<std::string, int> data;
};



现在我可以使用前者,因为当我有一个初始化列表构造函数时,整个初始化列表被视为一个元素,而不是被分割成元素。但我认为这个独立的需要的构造函数是死的丑陋。

Now I can use the former, because when I have an initializer-list constructor, the whole initializer list is treated as one element instead of being splitted into elements. But I think this separate need of the constructor is dead ugly.

我在寻找指导:


  • 前和后的初始化形式?

  • 在这种情况下,是否考虑添加初始化列表构造函数的要求?

如果你同意我以前的初始化方式更好,你能想到什么解决方案?

If you agree with me on that the former way of initialization is nicer, what solutions can you think of?

推荐答案


由于我有一个类似地图的类,并且初始化器有一个映射列表的抽象值,我想使用以前的版本

Since I have a map-like class, and the initializer has the abstract value of a mapping-list, I would like to use the former version

问题在于: 可以提供允许 类作为地图处理。你叫你的解决方案是一种解决方案,但没有什么可解决的。 :)

And herin lies the problem: it's up to you to supply the constructors that allow your class to be treated like a map. You called your solution a work-around, but there's nothing to work around. :)


但我认为这个构造函数的单独需要是死的。

But I think this separate need of the constructor is dead ugly.

它是,但不幸的是,因为它是你的类,你必须指定初始值设置列表的工作方式。

It is, but unfortunately since it's your class, you have to specify how the initializer lists work.

这篇关于从初始化器列表初始化,但没有{{{{{{{{...}}}}}}}}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆