static_assert on initializer_list::size()

前端 未结 5 1312
执笔经年
执笔经年 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:49

    Use following syntax:

    LIVE DEMO

    #include 
    
    template
    class Point
    {
        std::initializer_list data;
    public:
        constexpr Point(std::initializer_list init)
            : data
            (
                init.size() == Length ?
                init : throw 0
            )
        {}
    };
    
    int main()
    {
        constexpr Point a{{1,2,3}};
        constexpr Point b{{1,2,3}}; // compile time error
    }
    

    Refer following SO.


    EDIT: Interesting that works on GCC 4.8.1, but does not work on Clang 3.4. Maybe this is related to constexpr of .size() (afaik, in C++14 it is constexpr).

提交回复
热议问题