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

前端 未结 6 1726
忘了有多久
忘了有多久 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 05:09

    Wouldn't something like this give the desired effect (MyMapLike can be constructed in any way that std::map can, but does not implicitly convert to std::map)?

    struct MyMapLike : private std::map
    {
        using map::map;
    };
    

    If it absolutely positively has to be a member, maybe use constructor perfect forwarding (I'm not sure about the exact syntax) along the lines of:

    struct MyMapLike
    {
        template
        MyMapLike(Initializers... init, decltype(new std::map(...init)) = 0)
          : data(...init)
        { }
    
    private:
        std::map data;
    };
    

提交回复
热议问题