9

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>;
}
Community
  • 1
  • 1
Lim Ta Sheng
  • 371
  • 3
  • 8

1 Answers1

15

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.

Sneftel
  • 40,271
  • 12
  • 71
  • 104
  • I've installed gcc and have version 8.2.0, but still face this error in visual studio - how do I fix it? – nelion Dec 29 '19 at 23:42