static_assert on initializer_list::size()

前端 未结 5 1298
执笔经年
执笔经年 2020-12-05 00:29

Why is std::initializer_list<_E>::size not allowable in a static_assert, even though it\'s declared as a constexpr in my libstdc++ (v. 4.6)?

For example, the foll

5条回答
  •  独厮守ぢ
    2020-12-05 00:55

    From my discussion with @Evgeny, I realized that this just works (with gcc 4.8 c++11) and may as well do the size check by only accepting a compatible size in the initializer list (in main).

    (code link: http://coliru.stacked-crooked.com/a/746e0ae99c518cd6)

    #include
    template
    class Point
    {
      public:
        Point(std::array init)
        {
    //not needed//      static_assert(init.size() == Length, "Wrong number of dimensions");
        }
    };
    
    int main()
    {
      Point q({1,2,3}); //ok
    //  Point q2({1,2,3,4}); //compile error (good!)
      Point q2({1,2}); // ok, compiles, same as {1,2,0}, feature?
      return 0;
    }
    

提交回复
热议问题