问题
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