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

前端 未结 6 1724
忘了有多久
忘了有多久 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:01

    What do you think about the former and latter form of initialization? Does it make sense to be required to have extra braces in this case?

    I think so. I think it would permit too much ambiguity to allow a constructor be called not just when the syntax matches that constructor, but when the syntax matches some constructor of the single argument to the constructor, and so on recursively.

    struct A { int i; };
    struct B { B(A) {}  B(int) {} };
    struct C { C(B) {} };
    
    C c{1};
    

    Do you consider the requirement for addition of an initializer list constructor in this case bad?

    No. It lets you get the syntax you want but without creating the problems that arise if we make the compiler search harder for a constructor to use.

提交回复
热议问题