Initializing from an initializer list, but without {{{{{{{{ … }}}}}}}}?

前端 未结 6 1727
忘了有多久
忘了有多久 2020-12-15 04:24

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

struct MyMapLike {
  MyMapLike(std::map

        
6条回答
  •  悲哀的现实
    2020-12-15 04:59

    You could initialize as follows:

    MyMapLike maps ( { { "One", 1 }, { "Two", 2 } } );
    

    The outer ( ... ) are now clearly simply to surround the constructor args, and it's easier to see that the outermost { ... } defines the 'list'.

    It's still a bit verbose, but it avoids the situation where { is being used to do two different things, which affects readability.

提交回复
热议问题