Errors when using constexpr-if: expected '(' before 'constexpr'

风流意气都作罢 提交于 2020-01-24 11:53:32

问题


I am trying to use if-constexpr to check something, but I encounter errors like

expected '(' before 'constexpr'

'else' without a previous 'if' "

So far i check there is nothing wrong with my codes

My compiling flag is g++ -std=c++17 main.cpp

#include <iostream>
template<typename T, typename Comp = std::less<T> >
struct Facility
{
template<T ... list>
struct List
{
    static void print()
    {
        std::cout<<"\""<<"Empty List"<<"\""<<"\n";
    }
};
template<T head,T ... list>
struct List<head,list...>
{
    static void print()
    {
        std::cout<<"\"" << head;
        ((std::cout << " " << list), ...);
        std::cout<<"\""<<"\n";
    }
};
template<unsigned N,typename AA>
struct RemoveFirst{};

template<unsigned N,T head,T ... Rest>
struct RemoveFirst<N,List<head,Rest...>>
{
    struct result
    {
        static void print()
        {   
            if constexpr (N == head)
            {
                std::cout<<"";
            }
            else
            {
                std::cout<<"\""<<head;
                ((std::cout << " " << Rest), ...);
                std::cout<<"\""<<"\n";
            }
        }
    };
 };
};
template<int ... intlist>
using IntList = typename Facility<int>::List<intlist...>;

int main()
{
 using IntFacility = Facility<int>;
 using List = IntList<2, 8, 2, 3, 5, 10, 8, 5>;
}

回答1:


Older versions of GCC (up to 6.x) which do not support the final version of C++17 will give that error, because they recognize constexpr as a keyword but do not understand the constexpr-if construct. Make sure your GCC is version 7 or later.



来源:https://stackoverflow.com/questions/51291617/errors-when-using-constexpr-if-expected-before-constexpr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!